Professional Documents
Culture Documents
Beginning Python 11
Lists
• List is a compound data type used to group
together other values. List items need not all have
the same datatype.
• Lists are created by using square brackets.
Ex:
>>> L1 = [0,1,2,3],
>>> L2 = ['zero', 'one']
>>> L1 = [0,1,2,3]
>>> breakfast = [ "coffee", "tea", "toast", "egg" ]
• You can add or delete items from a list:
breakfast.append(“waffles”)
breakfast.remove(“coffee”)
Beginning Python 12
Types and Operators: Operations on Lists (1)
Some basic operations on lists:
Indexing: L1[i], L2[i][j]
Slicing: L3[i:j]
Concatenation:
>>> L1 = [0,1,2]; L2 = [3,4,5]
>>> L1+L2
[0,1,2,3,4,5]
Repetition:
>>> L1*3
[0,1,2,0,1,2,0,1,2]
Appending:
>>> L1.append(3)
[0,1,2,3]
Sorting:
>>> L3 = [2,1,4,3]
>>> L3.sort()
[1,2,3,4]
Types and Operators: Operations on Lists (2)
More list operations:
Reversal:
>>> L4 = [4,3,2,1]
>>> L4.reverse()
>>> L4
[1,2,3,4]
Index and slice assignment:
>>> L1 = [0,1,2]
>>> L2 = [3,4,5]
>>> L1[1] 1
>>> L2[0:2] [3,4]
Tuples
• A Tuple is a sequence of data type similar to List. A tuple consist of a
number of values separated by commas and enclosed witin parenthesis.
• Unlike list, the elements of a tuple can’t be changed, so it is a read only
list.
Ex:
>>> L1 = (0,1,2,3])
>>> L2 = ('zero', 'one‘)
>>> L1 = [0,1,2,3]
>>> print L1
0,1,2,3
>>> print (L1[1])
1
>>> tuple1 = ("This", "is", "a", "tuple")
Print(tuple1[1])
is
Beginning Python 15
Types and Operators: Tuples
Basic properties:
Concatenation:
>>> t1 = (0,1,2,3); t2 = (4,5,6)
>>> t1+t2
(0,1,2,3,4,5,6)
Repetition:
>>> t1*2
(0,1,2,3,0,1,2,3)
Length: len(t1) (this also works for lists and strings)
Dictionaries
• It is a mapping data type or a kind of hash table
that maps keys to values.
• Keys in a Dictionary can be of any data type.
• Keys in a Dictionary can be of any data type.
• Dictionaries are created using braces
Ex.
>>>sales = {‘apple’:100, ’orange’:[120,90]}
>>>print (sales.items()) It print all the items in the dictionary
>>>print (sales.keys()) It print all the keys in the dictionary
>>>print (sales.values()) It print all the values in the dictionary
>>>print (sales[‘apple’]) It print the price of apple
17
Dynamic Input
• Input() : Reads a number from user input.
• You can assign the return value by input into a
variable.
• Example:
age = input("How old are you? ")
print "Your age is", age
print "You have", 65 - age, "years until
retirement"
Output:
How old are you? 53
Conditional Statements
If (Cond): If (Cond):
statement1 statement1
statement2
statement2
else:
statement1
statement2
Conditional Statements
If (Cond): while (Cond):
statement1 statement1
statement2 statement2
elif (cond):
statement1
statement2
else: for var_name in
group_of_values:
statement1 <statements>
statement2
if Condition
If (Cond): Example:
statement1 x = 5
statement2 if x > 4:
print("x is greater than
4")
print("Bye")
Beginning Python 21
if Condition
If (Cond): Example:
statement1 x = 3
statement2 if x > 4:
print("x is greater than
4")
print("Bye")
Beginning Python 22
if/else Condition
If (Cond):
statement1
statement2
else:
statement1
statement2
Beginning Python 23
if/else Condition
• Example: WAP to check whether a number is
even or odd.
x = 3
if (x%2==0):
print("Number is even")
else:
print ("Number is odd")
Output:
Number is odd
Beginning Python 24
if/else Condition
• Example: WAP to check whether a number is
even or odd.
x = input("Enter a number:")
if (x%2==0):
print("Number is even")
else:
print ("Number is odd")
Output:
Enter a number: 12
Number is even
Beginning Python 25
for Loop
for var_name in group_of_values:
<statements>
Beginning Python 27
Range Function
• The range function specifies a range of
integers:
range(start, stop)
Integers range is in between start (inclusive) and
stop (exclusive)
Beginning Python 28
Use of Range Function in for loop
Example: Example:
for(i = 1 ; i <5; i+ for i in range
+) (1,5):
{ print i
printf("%d\n",i);
}
Output: Output:
1
1
2
2
3
3
4
4
Beginning Python 29
Use of Range Function in for loop
Example: Example:
for(i = 1 ; i <10; for i in range
i=i+2) (1,10,2):
{ print i
printf("%d\n",i);
}
Output: Output:
1 1
3 3
5 5
7 7
9 9
Beginning Python 30
for Loop
Example: Example:
for x in range (1,6): for x in range (1,6,2):
print x, "squared is", x * print x, "squared is", x*x
x
Output:
Output:
1 squared is 1
1 squared is 1 3 squared is 9
2 squared is 4 5 squared is 25
3 squared is 9
4 squared is 16
5 squared is 25
Beginning Python 31
for Loop
Example:
x= [1,2,3,4]
for i in x:
print i
Output:
1
2
3
4
Beginning Python 32
while Loop
Syntax:
Example:
while (Cond):
number = 1
statement1 while number < 5:
statement2 print number
number = number * 2
Output:
1
2
4
Beginning Python 33
While Loop
Example: Write a program o print first N
natural number
Beginning Python 34
While Loop
Example: Write a program o print first N
natural number
Output:
1
20
hi
Beginning Python 36
Functions in Python
A function in python begins with keyword def followed by function
name and parenthesis.
Defining a function
Output: 7
Output: 7
Functions in Python (Contd..)
Output: 7 Output: 7
Functions in Python (Contd..)
Output: HelloHi
Functions in Python (Contd..)
def add(a,b):
return a+b
Output:
10
10
Functions in Python (Contd..)
Showing function return multiple vales
def greater(x,y):
if x>y:
return x,y
else:
return y,x
Val= greater(10,100)
Print val
Output: (100,10)
Modules in Python
• Modules refers to a file containing python
statements and definitions.
• If a python file (say demo.py) is called a
module then its module name would be
demo.
• We can define our most used function in
a module and import it instead of copying
their definitions into different programs.
Modules in Python
Syntax:
Creating Module: import module_name
import demo
def add (a,b):
return (a+b)
def display():
program.py
print "Bye"
demo.py
Modules in Python
Syntax:
Creating Module: import module_name
import demo
def add (a,b):
x=2
return (a+b) print x
def display():
program.py
print "Bye"
demo.py
Modules in Python
Syntax:
Creating Module: import module_name
import demo
def add (a,b):
x=2
return (a+b) print x
demo.display()
demo.add(3,4))
def display():
program.py
print "Bye"
demo.py
Modules in Python
Syntax:
Creating Module: import module_name as
newname
import demo as
prog
def add (a,b):
return (a+b)
def display():
program.py
print "Bye"
demo.py
Modules in Python
Syntax:
Creating Module: import module_name as
newname
import demo as
prog
def add (a,b):
return (a+b) x=2
print x
prog.display()
def display():
print(prog.add(3,
program.py
print "Bye" 4))
demo.py
Modules in Python
Syntax:
Creating Module: from module_name import
func_name
from demo import
display
def add (a,b):
return (a+b)
def display():
program.py
print "Bye"
demo.py
Modules in Python
Syntax:
Creating Module: from module_name import
func_name
from demo import
display
def add (a,b):
return (a+b) demo.display()
def display():
program.py
print "Bye"
demo.py
Modules in Python
Syntax:
Creating Module: from module_name import
func_name
from demo import
display
def add (a,b):
return (a+b) demo.display()
demo.add(3,4) ×
def display():
program.py
print "Bye"
demo.py
Package in Python
• Package consist of modules and sub
packages.
module2.p
y
module2.py
a() __init__.p
y
b()
__init__.py can either be an empty file or
contain some initialization code for the
package.
Program to check a number is
prime or not
def isprime(n)
if n==1: Output:
return False; Enter a number: 5
for I in range(2,n/2): Number is Prime
if (n%2)==0:
return False Enter a number: 4
return True Number is not Prime
else:
print "Number is not Prime"
Beginning Python 57