Python tutorials for beginners : Part - I

in #utopian-io7 years ago (edited)

What Will I Learn?
This tutorial covers these topics:

  • Python basics
  • Strings in python
  • Numeric data in Python
  • Booleans and decision making

Requirements

  • A PC/laptop with any Operating system such as Linux, Mac OSX, Windows OS
  • Preinstalled Python
  • Preinstalled Code Editors such as Atom, Sublime text or Pycharm IDE

Note: This tutorial is performed in Pycharm IDE in laptop with Ubuntu 17.1, 64 bit OS

Difficulty
Basic, anyone with prior programming knowledge or newbie can catch up this tutorial.

Tutorial Contents
In this series of tutorials, we will learn basics of Python programming and we will move to advance level in future. This is the first part of this series.

What is Python?

  • Python is a powerful object-oriented, high-level programming language. It is easy to use, interpreted and interactive.

Your first Hello World program

#First Hello World Programm
print("Hello World")

The first line of above code is a comment which starts with '#'. Second line prints 'Hello World' when we run it.
If you have prior knowledge of any other programming language and wondering why the statement is not ended with a semicolon (;) then Python doesn't need a semicolon(;) to end a statement. It absolutely works on white space between statements. Correct indention is needed in python.

Comments: Comments are used to keep track of your code. We notate our work with comments so that it will be easy for some other person to understand the code and also easy for us to understand and remember what we have done in the code.

Single line comments in python start with '#'.

#This is single line comment

Multilines comment in python is enclosed with delimiter(''' ''')

'''This is multiple
line comment.
Hurray, I made it.'''

Comments are not executed while running program.
Screenshot

Variables: Variables are used to store values in reserved memory locations In python variables are declared automatically when we assign values using an equal sign (=). They don't need explicit declarations. We can store different types of data in variables. Python has five standard data types.
They are: Numbers, String, List, Tuple & Dictionary

Working with Strings:
A string is a sequence of characters. Strings are enclosed within single quotes('String') or double quotes("String") while they are created. Using single or double quotes doesn't make much difference except some cases.

print('Hello World')
print("Hello World")

Above two lines executes smoothly.

print('Hello's World')

but above line produces the error: SyntaxError: invalid syntax. This is due to python takes as strings only between ('Strings'). This can be avoided using double quotes or backslash().

print("Hello's World")
print('Hello\'s World')

Now, we will assign string value to a variable and perform different operations in it using inbuilt functions or method in it. Index of string starts with Zero(0).
Functions are the unit of lines of code which allows us to reuse the code.

Multiline strings is declared using triple quotes.

mstring ="""defining 
       multiline
       string """

Now

sent = 'First Variable String'

We declared variable sent in above line. As already mentioned, we don't need to declare explicitly in Python.
Now we will discuss some of the inbuilt functions of python for strings.

len()

gives the length of the string.

str[x]

gives the x'th letter of string 'str'.

lower()

converts all leter of strings to lowercase and upper() converts to uppercase.

max(str)

gives the max alphabetical character of string str and min(str) gives the min alpabetical value.

Adding two strings concatenete them.

String3 = string2 + string1
String3 = string2string1 


For more details in strings in python see this documentation.

Working with Numbers
Python supports four different numerical types. They are: int(positive or negatative numbers without decimal), long(integers of unlimited size), float(real numbers with a decimal point), complex(combination of real niumbers and imaginary numbers,form:a + ib, where a and b are floats and i is the square root of -1 which is imaginary number).

Python interpreter acts as simple calculator.

Some of the inbuilt python functions for numbers.

 int(a) 

converts a to integer, long(b) converts b to long integer, float(c) converts c to float and complex(x, y) converts x and y to complex number.

abs(a)

gives absolute value of a

exp(a) 

gives exponential of a

sqrt(a)

gives square root of a

Booleans:
Boolean is a data typewith only two values True and False.

Decision Making:
Decision making in programming refers to the executing of the program , according to the conditions.
Python has three types of decision making statements. They are if, if...else and nested if.

len = 21
if ( len == 21 ) : 
print "Length is 100"

In above lines of code, value of len is 21 thus, the condition is met and print function is executed.

x = 10
if x < 0:
     print 'X is greater'
 elif x == 0:
     print 'Zero'
 elif x > 2:
   print 'No'
else:
   print 'Yes'

When above line of code is executed " elif x > 2:"mets the condition and and "No" is printed.

nested if refers to wrapping of one if or else if inside another if or else if statement/statements.
Above codes can be found on my github link.

For more details visit Python docs.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

@programminghub, Approve is not my ability, but I can upvote you.

Thank you for the contribution. It has been approved.

For your next tutorial I recommend keeping everything consistent (sometimes you use print statements without brackets and sometimes with) and also make sure that the indentation is correct in the blocks of code you paste.

You can contact us on Discord.
[utopian-moderator]

@amosbastian Ok, Thanks, I will take care about it from next time.

Hey @programminghub I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 62816.09
ETH 2466.13
USDT 1.00
SBD 2.64