Introduction to Python Programming Language

List of Topics Covered:

  1. String
  2. List [ ]
  3. Dictionary{ }
  4. Tuple ( )
  5. Operators
  6. Control Statements
  7. Exception
  8. Function
  9. Module
  10. Class
  11. File I/O

1. String

  1. Group of characters enclosed in single quote or double quote.

  2. Example: str = "Beautiful"

  3. Individual character can be accessed using indexing.

  4. Indexing is possible in both direction (i.e from left and from right)

  5. From left index starts at 0.

  6. From right index starts at -1.

  7. String is immutable (in place replacement is not allowed)


#Displaying individual character 

str = "Beautiful"

#left indexing

print(str[0]) print(str[5]) #right indexing print(str[-2]) print(str[-6])

B
i
u
u

[6]

0s



#Displaying slice 
str = "Beautiful"
#left indexing
print(str[0:1])
print(str[0:3])
#right indexing
print(str[-2:])
print(str[-6:])
B
Bea
ul
utiful

Double-click (or enter) to edit


2. List

  1. List is a compound data type.

  2. It contains Group of comma (,) separate values of same or different types.

  3. Example:

         colour =["Red","Green","Blue"] 
         mixed = ["one",2,3.0,"Four"]     
  4. Individual element of list (element) can be accessed using indexing.

  5. Indexing is possible in both direction (i.e from left and from right)

  6. From left index starts at 0.

  7. From right index starts at -1.

  8. List is mutable (in place replacement is allowed)


[10]

0s



#Displaying individual element of a list 
colour=["Red","Green","Blue"]
print(colour[1])
print(colour[2])
print(colour[-3])
Green
Blue
Red

[9]

0s

#Displaying slice of a list (group of elements) 

colour=["Red","Green","Blue"]

print(colour[1:2])

print(colour[1:])

print(colour[-3:])

['Green']
['Green', 'Blue']
['Red', 'Green', 'Blue']

3. Dictionary

  1. Dictionary is used for mapping.

  2. Each item in the dictionary is in the form of key:value pairs and is separated by comma (,).

  3. Example:

         colour ={0:"Red",1:"Green",2:"Blue"} 
         mixed = {"one":1,2:"two",3.0:"Three"}     
  4. Individual element of dictionary (item) can be accessed using its 'key'.

  5. In the above example 0,1,2 are called as keys & "Red","Green","Blue" are their values respectively.


[18]

0s



di = {0:"Red",1:"Green",2:"Blue"}  
print(di)
print(di[1])
print(di[2])
#print(di[-2])   #key error
#print(di[0:2]) #----unhashable type 'slice'
print(di.keys())
print(di.values())
{0: 'Red', 1: 'Green', 2: 'Blue'}
Green
Blue
dict_keys([0, 1, 2])
dict_values(['Red', 'Green', 'Blue'])

[20]

0s



mixed = {"one":1,2:"two",3.0:"Three"}
print(mixed)
print(mixed["one"])
print(mixed[2])

print(mixed.keys())
print(mixed.values())
{'one': 1, 2: 'two', 3.0: 'Three'}
1
two
dict_keys(['one', 2, 3.0])
dict_values([1, 'two', 'Three'])

4. Tuple

  1. Tuple is a compound data type.

  2. It contains Group of comma (,) separate values of same or different types.

  3. Example:

         colour =("Red","Green","Blue") 
         mixed = ("one",2,3.0,"Four")     
  4. Individual element of Tuple can be accessed using indexing.

  5. Indexing is possible in both direction (i.e from left and from right)

  6. From left index starts at 0.

  7. From right index starts at -1.

  8. Tuple is immutable (in place replacement is not allowed).However it may contain mutable elemet.


[21]

0s



#Displaying individual element of a Tuple 
colour=("Red","Green","Blue")
print(colour[1])
print(colour[2])
print(colour[-3])
Green
Blue
Red

[22]

0s



#Displaying slice of a list (group of elements) 
colour=("Red","Green","Blue")
print(colour[1:2])
print(colour[1:])
print(colour[-3:])
('Green',)
('Green', 'Blue')
('Red', 'Green', 'Blue')

Packing and unpacking in Tuple


[24]

0s



t=10,10.5,"hello" #packing
print(t)
x,y,z=t;          #unpacking
print(x)
print(y)
print(z)
(10, 10.5, 'hello')
10
10.5
hello

5. Operators

  1. Arithmetic operators (+ - * / // % **)

         Example:
         a1 = 4+3*2-1
         b1 = 7/5
         c1 = 7//5
         d1 = 7%5
         e1 = 2**3  
  1. Relational operators (== != < <= > >= )

         Example:
         a1 = 4==3
         a2 = 4!=3
         b1 = 4<3
         b2 = 4<=3
         c1 = 4>3
         c2 = 4>=3     
  2. Bitwise Operators (& | ^ ~ << >>)

         Example:
         a1 = 4&3
         b1 = 4|3
         c1 = 4^3
         d1 = ~3
         e1 = 4<<3
         f1 = 4>>2
  1. Logical Operators (and or not)

         Example:
         a1 = True and True
         b1 = True or False
         c1 = not True
  2. Assignment Operators ( = += -= *= /= )

         Example:
         a1 = 4
         a1+=3 #a1 = a1 + 3
         a1-=1
         a1*=3
         a1/=3
  3. Unary Operators (+ - ~)

         Example:
         a = 5
         a1 = +a
         b1 = -a
         c1 = ~a
  4. Membership Operator ( in, not in)

         Example:
         list1 = [1,2,3,4,5]
         a = 5
    
         a1 = a in list1
         a2 = a not in list1
  5. Identity Operator ( is, is not)

         Example:
         list1 = [1,2,3,4,5]
         list2 = [1,2,3,4,5]
         list3 = list1
    
         a1 = list1 is list2
         a2 = list1 is not list2
         a3 = list1 is list3
         a4 = list1 is not list3

Operator Precedence & order of evaluation:

  1. Exponent ( ** )

  2. Unary ( ~ + - )

  3. multiply, divide, modulo, floor division ( * / % // )

  4. add, substract ( + - )

  5. left shift, right shift ( << >> )

  6. bitwise and ( & )

  7. bitwise xor, bitwise or ( ^ | )

  8. comparison ( < <= > >= )

  9. equality ( == != )


6. Control Statements

Statements:

  1. Sequential statements Example: Statement 1 Statement 2 Statement 3

  2. *Decision Making statements *(branching) Example: if (condition): statement1 statemment2 statement3

    if (condition): statement1 statemment2 else: statement3 statement4 statement5

    if (condition1): statement1 statemment2 elif(condition2): statement3 statement4 elif(condition3): statement5 else: statement6 statement7

  3. *Iterative statements *(looping) Example: while(condition): statement1 statement2 statement3

    for i in sequence: statement1 statement2 statement3


[31]



# if..else
day=input("Enter today's day")
if(day=="sunday"):
  print(day,"is","holiday")
else:
  print(day,"is","working day")
Enter today's daymonday
monday is working day

[33]

5s



# if..elif..else
day=input("Enter today's day")
if(day=="sunday"):
  print("Today's desert is Basundi")
elif(day=="monday"):
  print("Today's desert is GulabJamun")
elif(day=="Tuesday"):
  print("Today's desert is Khir")
elif(day=="wednesday"):
  print("Today's desert is PuranPoli")
elif(day=="thursday"):
  print("Today's desert is Srikhand")
elif(day=="friday"):
  print("Today's desert is Aamrakhand")
elif(day=="saturday"):
  print("Today's desert is Kaju Katali")
else:
  print("You have entered wrong day")
Enter today's dayMonday
You have entered wrong day

[29]

0s



a=5
while(a>0):
  print("a = ",a)
  a=a-1
print("now you are outside the loop")

a =  5
a =  4
a =  3
a =  2
a =  1
now you are outside the loop

[27]

0s



list1=[1,2,3,4,5]
for i in list1: 
  print(i,"  ",i**2)
1    1
2    4
3    9
4    16
5    25

0s

*values: object, hint

Copied 4 cells. You can now paste them in this or a different notebook.