2. Lab Objectives
To write, test, and debug simple Python programs.
To use operators in a python program.
To implement Python programs with conditionals.
To implement Python programs with loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, and
dictionaries.
3. Lab Outcomes
Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.
Use operators in a python program.
Implement Python programs with conditionals.
Implement Python programs with loops.
Develop Python programs step-wise by defining functions and calling
them.
Use Python lists, tuples, dictionaries for representing compound data.
4. Download and Install Python Setup
Download the setup from the below link
http://python.org/downloads/
Installation steps
Step 1: Select Version of Python to Install.
Step 2: Download Python Executable Installer.
Step 3: Run Executable Installer.
Step 4: Verify Python Was Installed on Windows.
Step 5: Verify Pip Was Installed.
Step 6: Add Python Path to Environment Variables (Optional)
5. What is python
Python is a popular programming language. It was created in
1991 by Guido Van Rossum.
It is used for
Web development
Software development
Data analysis
Game development
Desktop application
Embedded application
Introduction to Python
6. Write python program to display hello world on the screen
i.e print("hello world")
Python First Program
7. Python Variables
Unlike other programming languages, Python has no command for
declaring variable.
A variable is created at the moment you first assign a value to it.
Example
X=5 # x is now type of int
Y= “Elias” # Y is now type of string
Print(x)
Print(y)
Variables do not need to be declared with any particular type and can even
change type after they have been set.
8. Python Variables Names
A variable can have a short name (like x and y) or amore descriptive name
like (age, fnam, lname, etc).
Rules for naming Python variables
A variable name must start with letter or underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores(A-Z, 0-9, and _)
A variable names are case sensitive (age, Age and AGE are three
different variables)
9. Continued
The python print statement is often used to output variables.
To combine both text and a variable, Python uses the + character:
Example
X=“Intereting.”
Print(“Python is “+ x)
The output will be
Python is Interesting.
10. Python Data Types
Write a program to demonstrate different
number data types and string in Python.
INPUT
a=10; #Integer Datatype
b=11.5; #Float Datatype
c=2.05j; #Complex Number
xy="Automotive Engineering"
print("a is Type of",type(a)); #prints type of variable a
print("b is Type of",type(b)); #prints type of variable b
print("c is Type of",type(c)); #prints type of variable c
print("xy is Type of",type(xy)); #prints type of variable xy
OUTPUT
a is Type of <class ‘int’>
b is Type of <class ‘float’>
c is Type of <class ‘complex’>
xy is Type of <class ‘str’>
11. Reading input from Keyboard
Write a python program that reads input(your full name) from keyboard and
displays the result . Type the following code.
Input
fname=str(input("Please enter your first name :"))
lname=str(input("Please enter your Last name :"))
print("Your full name is" "n"+fname + " " +lname)
Output
Please enter your first name :ELIAS
Please enter your Last name :PETROS
Your full name is
ELIAS PETROS
12. ARTHIMETIC OPERATIONS
INPUT
Write a Python Program that accepts two numbers and make
d/t arithmetic on it
a = int(input ('Enter the first number:'))
b = int(input ('Enter the second number:'))
summ=a+b
mul=a*b
div=a/b
fdiv=a//b
modu=a%b
print("Addition of a and b is :" +str(summ))
print("Multiplication of a and b is :" +str(mul))
print("Division of a and b is :" +str(div))
print("Floar Division of a and b is :" +str(fdiv))
print("Modulo devision of a and b is :" +str(modu))
OUTPUT
Enter the first number:24
Enter the second number:13
Addition of a and b is :37
Multiplication of a and b is :312
Division of a and b is :1.8461538461538463
Floar Division of a and b is :1
Modulo devision of a and b is :11
18. Python String Methods and Functions
Write a program to create, concatenate and
print a string and accessing sub-string from a
given string.
INPUT
s1=input("Enter first String : ");
s2=input("Enter second String : ");
print("First string is : ",s1);
print("Second string is : ",s2);
print("concatenations of two strings :",s1+s2);
print("Substring of given string :",s1[1:4]); # prints strin
g from 1 to 3 because array Indexing starts from 0 and
ends at n-1.
OUTPUT
Enter first String: COMPUTER
Enter second String: SCIENCE
First String is : COMPUTER
Second String is : SCIENCE
Concatenations of two strings :
COMPUTERSCIENCE
Substring of given string: OMP
19. Python Conditional (If ) Statement
Write a python program which accept one integer number
from keyboard then display the number when number is
greater than 0 using python if statement.
INPUT
a = int(input ('Enter the number:'))
if a > 0:
print (a, "is greater than 0")
OUTPUT
It displays a if the entered
number is greater than 0
Unless it cant display anything.
20. Python Conditional(If-Else) Statements
Write a python program which accept two integer
numbers from keyboard then display which number is
greater using python if-else statement.
INPUT
a = int(input ('Enter the first number:'))
b = int(input ('Enter the second number:'))
if a > b:
print (a, "is greater than ", b)
else:
print (b, "is greater than ",a)
OUTPUT
Enter the first number:12
Enter the second number:23
23 is greater than 12
21. Python Conditional(If-Elif- Else ) Statements
Write a python program which takes numbers from
keyboard then display the number is either positive,
negative or 0 using python if-elif-else statement.
INPUT
number=int(input("Please enter the number you want to check
?"))
if number>0:
print("The number you entered is positive")
elif number<0:
print("The number you entered is negative")
else :
print("The number you entered is zero")
OUTPUT
Please enter the number you
want to check ?32
The number you entered is
positive
22. Write a python sources code that accept students score then calculate the grade
of the score?
Note:- Score range and grade were provided as bellow
Score range: Grade :
90-100 ===>A
80-89 ===>B
70-79 ===>C
60-69 ===> D
Below 60 ===>F
Python Conditional Nested(If-Elif- Else ) Statements
23. Continued
INPUT
score=float(input("Please enter your score:"))
if score>=90:
print("Grade = A ")
elif score>=80:
print("Grade = B ")
elif score>=70:
print("Grade = C ")
elif score>=60:
print("Grade = D ")
else :
print("Grade = F ")
OUTPUT
Please enter your score:34
Grade = F