Practical # 1
Practical # 1
# UNDERSTAND
TO 1 BASICS OF
PYTHON
QUOTATION IN
PYTHON
COMMENTS IN
PYTHON
•Taking User Input
input(“waiting for user input”)
MULTIPLE
ASSIGNMEN
T
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
COMPARISO
N
OPERATORS
These operators
compare the
values on either
sides of them
and decide the
relation among
them. They are
also called
Relational
operators.
PYTHON
ASSIGNMEN
T
OPERATORS
PYTHON
MEMBERSHIP
OPERATORS
Python’s
membership
operators test for
membership in
a sequence,
such as strings,
lists, or tuples.
EXAMPL
E
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“)
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
if=a15 b:
b
>
print("a greater")
is
elif == b:
a
print("bot are
Output:hboth are equal")
else:
equal
print("b greater")
is
LOOP
S
LOOP
S
•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))
Loop executio 0
Output:
n
Loop executio 1
n
Loop executio 2
n
WHILE
Syntax:
LOOP
while expression: #execute your
code
Example:
#initialize count variable
to 1 count =1
while count < 6 :
print (count)
#the
count+=1 line means = count +
above count 1
Outpu
t:
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):
1 * 1 print
= 1 ("%d *
1 * 2 =%d2 = %d" %
2 * 1 =( 2g, k, g*k))
Output:
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
5 * 1 = 5
5 * 2 = 10
LOOP
CONTROLS
LOOP
CONTRO
L
•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
Syntax:
STATEMENT
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
CLASS
TASKS:
•Write a marksheet and display marks obtained, percentage and grade
•Write a program for table
Class task 3:
LAB # 1:
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:11/09/2024