Practical # 1
Practical # 1
EMAN SHAHID
INSTALLATION
• Easy-to-read
• Easy to learn
• Open Source
• Portable − Python can run on a wide variety of hardware platforms and has
the same interface on all platforms.
FEATURES
• Store Python script source code in a file with the .py extension, and use the
interpreter to execute the contents of the file. To execute the script by the
interpreter, you have to tell the interpreter the name of the file. For example,
if you have a script name MyFile.py
SYNTAX
SYNTAX
• Python provides no braces to indicate blocks of code for class and function
definitions or flow control. Blocks of code are denoted by line indentation,
which is rigidly enforced.
if True:
print "True"
else:
print "False"
MULTI-LINE STATEMENTS
QUOTATION IN PYTHON
COMMENTS IN PYTHON
• Taking User Input
input(“waiting for user input”)
MULTIPLE
ASSIGNMENT
PYTHON OPERATORS
PYTHON OPERATORS
Operators are the constructs which can manipulate the value of operands.
Types of Operator
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
PYTHON
ARITHMETIC
OPERATORS
PYTHON
COMPARISON
OPERATORS
These operators
compare the values
on either sides of
them and decide the
relation among them.
They are also called
Relational operators.
PYTHON
ASSIGNMENT
OPERATORS
PYTHON
MEMBERSHIP
OPERATORS
Python’s membership
operators test for
membership in a
sequence, such as
strings, lists, or tuples.
EXAMPLE
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print ("Line 1 - a is available in the given list“)
else:
print ("Line 1 - a is not available in the given list“)
• Identity operators
compare the
memory locations of
two objects
EXAMPLE
OUTPUT
BITWISE
OPERATORS
• Bitwise operator
works on bits and
performs bit by bit
operation. Assume
if a = 60; and b =
13;
LOGICAL
OPERATORS
DECISION MAKING
DECISION
MAKING
STATEMENT
IF STATEMENT
Syntax:
if expression: #execute your code
Example:
a = 15
if a > 10:
print("a is greater")
Output: a is greater
IF ELSE STATEMENT
Syntax:
if expression: #execute your code
else: #execute your code
Example:
a = 15
b = 20
if a > b:
print("a is greater")
else:
print("b is greater")
Output: b is greater
ELIF STATEMENTS
Syntax:
if expression: #execute your code
elif expression: #execute your code
else: #execute your code
Example:
a = 15
b = 15
if a > b:
print("a is greater")
elif a == b:
print("both are equal")
else:
print("b is greater")
Output: both are equal
LOOPS
LOOPS
• For loop
• While loop
• Nested loop
FOR LOOP
Syntax:
for iterating_var in sequence: #execute your code
Example 01:
for x in range (0,3) :
print ('Loop execution %d' % (x))
Output:
Loop execution 0
Loop execution 1
Loop execution 2
WHILE LOOP
Syntax:
while expression: #execute your code
Example:
#initialize count variable to 1
count =1
while count < 6 :
print (count)
count+=1
#the above line means count = count + 1
Output:
1
2
3
4
5
NESTED LOOP
Syntax:
for iterating_var in sequence:
for iterating_var in sequence: #execute your code #execute
your code
Example:
for g in range(1, 6):
for k in range(1, 3):
print ("%d * %d = %d" % ( g, k, g*k))
Output:
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
5 * 1 = 5
5 * 2 = 10
LOOP CONTROLS
LOOP
CONTROL
• These statements
are used to change
execution from its
normal sequence.
BREAK STATEMENT
Syntax:
break
Example:
count = 0
while count <= 100:
print (count)
count += 1
if count >= 3:
break
Output:
0 1 2
CONTINUE STATEMENT
Syntax:
continue
Example:
for x in range(10): #check whether x is even
if x % 2 == 0:
continue
print (x)
Output:
1
3
5
7
9
PASS STATEMENT
Syntax:
pass
Example:
for letter in ‘Python’:
if letter == ‘y’:
pass
print ('Pass block’)
print ('Current letter is:', letter)
Output:
Current letter is : P
Pass block
Current letter is : y
Current letter is : t
Current letter is : h
Current letter is : o
Current letter is : n
CLASS TASKS::
• Write a Python program which accepts the radius of a circle from the user
and compute the area.
• Write a Python program to guess a number between 1 to 9. Hint(use radom
method to randomly generate numbers).
• Write a Python program that accepts a word from the user and reverse it.
• Write a Python program that prints all the numbers from 0 to 6 except 3 and
6. Note : Use 'continue' statement.
• Write a Python program which iterates the integers from 1 to 50. For
multiples of three print “Multiple of 3" instead of the number and for the
multiples of five print “Multiple of 5". For numbers which are multiples of both
three and five print “Multiple of both 3 & 5".
• Write a Python program to check whether an alphabet is a vowel or
consonant.
• Write a Python program to convert temperatures to and from celsius,
fahrenheit.
LAB SUBMISSION DATE: 20/4/2018