All you need to know about python

1.Introducing python

What is python ?

  • It is an interpreted, high-level, general-purpose scripting language
  • Created by Guido van Rossum, and released in 1991

Advantages of python:

  • Easy to learn and use
  • Improved productivity
  • Free & open-source
  • Massive libraries

Disadvantages of python:

  • Slow speed
  • Memory inefficient
  • Weak mobile computation
  • Runtime errors

2. Operators in python

Arithmetic Operators: (used for mathematical operations)

+        Addition 
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor division

Comparison Operators: (used to compare two values)

==       Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Logical Operators: (used to combine conditional statements)

and      Returns true if both statements are true
or Returns true if one of the statement is true
not Reverse the result return false if the result is true

Identity Operators: (used to compare object with same object & same memory location)

is      Return true if both the variable are same
is not Return true if both variable are not same

Membership Operators: (used to test if a value is presented in an object)

in      Returns true if a value is present in the object
not in Returns true if a values is not present in the object

3. Variables in python

  • a container or a bag to store a data (or) a value in it
  • can be changed whenever we need it to be
  • values with the variable is stored as an cache
x = 10#changing the valuex = 5  #now the value of 10 will be changed into 5

4. Datatypes in python

  • Since everything in python is object
  • Every value in python has a datatype
  • Numeric types (int, float, complex)
  • Sequential types (list, tuple, range)
  • Set types (set, frozenset)
  • Mapping types (dictionary)

Numbers:

  • integer (int)
  • floating point number (float)
  • complex numbers (complex)

Examples:

 a = 3     (int)
b = 4.5 (float)
c = 1 +2j (complex)

List:

  • mutable ( add, del, update, insert, etc..)
  • set of numbers and components
  • items are separated by commas and are enclosed in []
list_1 = [1,2,3]           #same-type
list_2 = [1,0.3,'shaw'] #different type
  • List has methods like append, extend, pop, insert
mylist = [1,2,3]mylist.append(4)     #takes one element and add at last of listmylist.extend(5,6,7) #add multiple valuesmylist.pop()         #last in first outmylist.insert(2,9)   #insert a specific element at specific index

Tuple:

  • immutable can’t add item to it
  • all items should be of same type & allows duplications
  • Tuples are written in round brackets ()
tup_1 = ('red', 'green', 'white', 'red') 
  • Tuple has only two methods to it index, count.
tup_1.count('red')  #returns the number of times the value is occurs in tupletup_1.index('white') #searches the tuple and return its index position

Range:

  • creates a sequence of number from the given range starting from 0 by default
  • Needs 3 arguments to be passed in
  • All 3 arguments can be either +ve or -ve integers
range(5)      #creates a number range from 0-5range(2,8)    #creates a number range from 2-7range(3,15,2) #creates a number range from 3-14, but increment by 2 instead of 1

Set:

  • Set is a collection of both unordered and unindexed
  • Does not allow duplications
  • Set are written using curly brackets { }
set_1 = {'cherry', 'apple', 'banana'}  
  • Set has also many methods add, union, intersection, clear, etc..
set_2  = {'apple', 'dates', 'orange'}set_1.add('grapes')       #add a new element to the existing setset_1.union(set_2)        #returns the common value set_1.intersection(set_2) #returns the intersected value set_2.clear()          #clears all elements and returns an empty set

Dictionary:

  • mentioned by key & values pairs
  • values can mutable and key cannot be mutable
  • created by a comma separated list of key:value pairs inside braces { }
a = {1:'red', 2:'blue' 3:'green'}   #dictionary
b = dict(one=1, tow=2, three=3) #another method
  • dictionary methods list, len, clear, keys, etc..
list(b)    #Return a list of all the keys used in the dictionary len(b)     #returns the len of the dictclear(a)   #returns an empty dict with all values deletedkeys(a)    #returns only the keys from the given dict

5. Conditionals statements:

Indentation

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.

If Statements:

An “if statement” is written by using the if keyword. Logical units play a main role in conditionals statements (True/False).

if expression:
statement
a = 3
b = 10
if b > a:
print("b is greater than a")

Elif Statements:

The elif keyword is pythons way of saying “if the previous conditions were not true, then try this condition”

if expression:
statement
elif expression:
statement
a = 43
b = 43
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")

Else Statements:

The else keyword catches anything which isn’t caught by the preceding conditions.

if expression:
statement
elif expression:
statement
else:
statement
a = 100
b = 56
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

Nested If statements:

You can have if statements inside if statements, this is called nested if statements.

if expression:
Statement
if expression:
Statement
x = 50

if x > 20:
print("Above 20,")
if x > 40:
print("and also above 40!")
else:
print("but not above 20.")

6. Loops in python:

Loops mean printing values as many times until the user needs. Python has two loops:

  • while loops
  • for loops

While loop:

i = 1
while condition:
Statement
increment/decrement

Main concept in loop is initialization (i = 1), Condition (a < b), Increment/decrement (++ /- -)

a = 1               initialization
while a <= 5: condition
print("hi")
a+=1 increment

For Loop:

  • for loops mostly used in sequence (list, tuples, sets, dict)
  • no need to specify condition
for i in var_name:
print(i)
  • as like while loop for loop is not used to print or repeat certain values
EXP1:a = [1,2,3,4,5]
for i in a:
print(i) # print all the values in the list of a one by one

EXP2:
for j in range(0,10,2):
print(j) #prints 0 to 10 at a step count of 2

Master graduate student who is ready to learn new stuffs and share my knowledge with everyone