Python Programming [ Lab Programs]
Lab Objectives:
✓ To write, test, and debug simple Python programs.
✓ To implement Python programs with conditionals.
✓ To implement Python programs with loops.
✓ Use functions for structuring Python programs.
✓ To use operators in a python program.
✓ Represent compound data using Python lists, tuples, and dictionaries.
Lab Outcomes: Upon completion of the course, students will be able to
✓ Write, test, and debug simple Python programs.
✓ Implement Python programs with conditionals.
✓ Implement Python programs with loops.
✓ Develop Python programs step-wise by defining functions and calling them.
✓ To use operators in a python program.
✓ Use Python lists, tuples, dictionaries for representing compound data.
Activity 1. Download and Install Python setup
1.1. Download the setup
http://python.org/downloads/
1.2. 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)
Activity 2. Running python programs
➢ Two modes for using the Python interpreter: - 1. Interactive Mode 2. Script Mode.
Activity 3. Python Comments
Activity 4. Reading input from the console
Example 1. ➔ Python program which accept your name from keyboard and display it.
str = input('Enter your name:')
Enter your name:
Raj kumar
print(str)
Output ➔ Raj kumar
Activity 5. Python Basic Operators
Example 1. Python program which accepts two numbers and perform different arithmetic on it
a=int(input("Enter a value:")); #input() takes data from console at runtime as string.
b=int(input("Enter b value:")); #typecast the input string to int.
print("Addition of a and b ",a+b);
print("Subtraction of a and b ",a-b);
print("Multiplication of a and b ",a*b);
print("Division of a and b ",a/b);
print("Remainder of a and b ",a%b);
print("Exponent of a and b ",a**b); #exponent operator (a^b)
print("Floar division of a and b ",a//b); # floar division
Enter a value: 10
Enter b value: 5
Output
Addition of a and b 15
Subtraction of a and b 5
Multiplication of a and b 50
Division of a and b 2.0
Remainder of a and b 0
Exponent of a and b 100000
Floar division of a and b 2
Activity 6. Python Data Types
1. Write a program to demonstrate different number data types and string in Python.
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’>
Activity 6. Python String methods and functions
1. Write a program to create, concatenate and print a string and accessing sub-string from a given string.
Source Code:
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]);
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
String functions and methods:
Activity 7. Python conditional structure / statement
1. 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.
Source code
a = int(input ('Enter the number:'))
if a > 0:
print (a, "is greater than 0")
Output
Exercise. Write a python program that accept age of a person then display a message "You are
eligible to vote " if and only if the age a person is greater than or equal to 18.
2. Write a python program which accept two integer numbers from keyboard then display which
number is greater using python if-else statement.
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)
Exercise. Write a python program that accept age of a person then display a message "You are
eligible to vote ". when that age a person is greater than or equal to 18 or “You are not eligible to
vote" when age is less than 18.
3. Write a python program which accept one number from keyboard then identify that number as
positive, negative, or zero using python if-elif-else statement.
a = input ('Enter the number:')
if a > 0:
print (a, "The number is positive")
elif a<0:
print (a, "The number is Negative")
else:
print (a, "The number is zero")
Exercise
1. Write a python sources code that accept string from the user and apply different string
functions and methods on it.
2. Write a python program that accept one integer number from keyboard (user) and display a
message only when the input number is greater than 100 using if statement condition.
== > message is: “The input number is greater than 100”
3. Write a python program that accept two number from keyboard (user) then compare them
(which number is greater) using if else statement condition.
4. Write a python program that take input from the user then identity the number as number is
even or odd using if else statement.
5. Write a python program that take one number from the user then identity the number as
number is positive, negative or zero using if-elif-else statement.
6. 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
7. Write a python program which accept your first name and last name and display your first
name and last name on single line?
Note: 1. store your first name on variable fname 2. store your last name on variable lname
and 3. Add space between your name and father name (first name and last name).