Learning Is Never Easy

As I said learning is never easy. At least for me. When I first started learning Python from youtube back in my college, Every time tutorials starts with Variables, Operators and Expressions. I thought it's more like C/C++ and then I used to pause the tutorial and procrastinate. I procrastinate so much that I procrastinate the actual procrastination.

Well later I realized that I've wasted the time. But then I've started again and this time through docs. Yes from PYM book and blogs. I think docs gave me a better picture. I'm still learning. It's not that thing that you can complete within a week. Better things take time. Here are few things for beginners who is getting started with Python.

Whitespaces and indentation:

Whitespace and indentation in Python is important. Whitespace in the beginning of the line called indentation. For wrong indentation Python throws an error. Example:

>>> a=10
>>>    b=20
  File "<stdin>", line 1
    b=20
    ^
IndentationError: unexpected indent

There are more places where we should be following the same type of whitespace rules:

Comments:

Comments are simple English to explain what this code does. It's easier to understand your code if you follow proper commenting for every code snippets. Comment line starts with # and everything after is considered as a comment. Example:

#This is a comment
a = 10
b=20
#This line will add two numbers
a+b

Multi Line Comments:

#This is a comment
#written in
#more than just one line
print("Hey there")

or we can add a multiline string (triple quotes) in our code, and place our comment inside it.

'''
This is a comment
written in
more than just one line
'''
print("Hey there") 

Modules:

Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. To create a module just save the code you want in a file with the file extension .py To use a module you have to import it first. Example:

>>> import math
>>> math.sqrt(16)
4.0

Keywords and Identifiers:

Following identifiers are used as a keywords and these can not be used as an ordinary identifiers. False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise

In Python we don’t specify what kind of data we are going to put in a variable. So you can directly write abc = 1 and abc will become an integer datatype. If you write abc = 1.0 abc will become of floating type. Example:

>>> a=10
>>> b=20.0
>>> type(a)
<type 'int'>
>>> type(b)
<type 'float'>

From the above example you can understand that to declare a variable in Python , what you need is just to type the name and the value. Python can also manipulate strings They can be enclosed in single quotes or double quotes like:

>>> 'Python is not a snake'
'Python is not a snake'
>>> "Python is a programming language"
'Python is a programming language'

Input from Keyboard:

Generally the real life Python codes do not need to read input from the keyboard. In Python we use input function to do input.

$ vim hello.py

number = int(input("Enter an integer: "))
if number < 100:
    print("Your number is smaller than 100")
else:
    print("Your number is greater than 100")

Output:

$ ./hello.py
Enter an integer: 229
Your number is greater than 100
$ ./hello.py
Enter an integer: 1
Your number is smaller than 100

Multiple assignments in a single line:

We can assign values to multiple variables in a single line:

>>> i, j = 100, 200
>>> a
100
>>> b
200

Operators and Expressions:

Operators are used to perform operations on variables and values. Python provides few operators which are below listed:

Arithmetic operator:

Arithmetic operators are used with numeric values to perform common mathematical operations:

>>> x=10
>>> y=10
>>> x+y #Addition
20
>>> x-y #Subtraction
0
>>> x*y #Multiplication
100
>>> x/y #Division
1.0
>>> x%y #Modulus
0
>>> x**y #Exponentiation
10000000000
>>> x//y #Floor division
1

Assignment operator:

Assignment operators are used to assign values to variables:

>>> x = 5
>>> x += 5
>>> x -= 5
>>> x *= 5
>>> x /= 5
Comparison operator:

Comparison operators are used to compare two values:

>>> x == y
>>> x != y
>>> x > y
>>> x < y
>>> x >= y
>>> x<= y

Logical operator:

and #Returns True if both statements are true
or #Returns True if one of the statements is true
not #Reverse the result, returns False if the result is true

Identity operator:

is #Returns true if both variables are the same object
is not #Returns true if both variables are not the same object

Membership operator:

in #Returns True if a sequence with the specified value is present in the object
not in #Returns True if a sequence with the specified value is not present in the object

Bitwise operator:

& #AND- Sets each bit to 1 if both bits are 1
| #OR- Sets each bit to 1 if one of two bits is 1
^ #XOR- Sets each bit to 1 if only one of two bits is 1
~ #NOR- Inverts all the bits
<< #Zero fill lest shift (Shift left by pushing zeros in from the right and let the leftmost bits fall off)
>> #Signed fill right shift (Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off)