[Unit II: Python Programming]
Program 1: Write a program that prints the message "Hello, World!
Flow Chart
Algorithm:
1. Start Start
2. Define a variable named message and
assign the string value "Hello, World!" to Declare Variable: message
it.
Assign value: message=”Hello, World”
3. Print the value of the message variable
to the console.
4. End print message
Stop
Output of Program:
[Unit II: Python Programming]
Program 1: Write a program that prints the message "Hello, World!
def main():
message="Hello, world"
print(message)
if __name__ == "__main__":
main()
[Unit II: Python Programming]
Program 2: Write a statement in Python IDLE to view the list of
Python keywords.
Flow Chart
Start
Algorithm:
1. Start
Import the keyword module:
2. Import the keyword module:
3. Access the kwlist attribute:
4. Print the kwlist: Access the kwlist attribute:
5. End
print kwlist
Stop
Output of Program:
[Unit II: Python Programming]
Date
Page No.
Program 2: Write a statement in Python IDLE to view the list of Python keywords.
import keyword # Importing the keyword module to access Python's reserved keywords
def main(): # Defining the main function
print(keyword.kwlist) # Printing the list of all Python keywords
if __name__ == "__main__": # Checking if this script is being run directly
main() # Calling the main function
[Unit II: Python Programming]
Program 3: Write a code that prints your full name and your birthday as separate strings in Python.
Flow Chart
Algorithm: Start
1. Start
2. Define a function named main. Define the main function
3. Inside the main function:
Print the full name: "Balwinder Singh Balli"
Print the birthday: "Birthday: April 8, 1980" Print the full name: "Balwinder Singh Balli"
4. call the main function. Print the birthday: "Birthday: April 8, 1980"
5. End
Call main Function
Stop
Output of Program:
[Unit II: Python Programming]
Date
Page No.
Program3: Write a code that prints your full name and your birthday as separate strings in Python.
def main(): # Defining the main function
print("Balwinder Singh Balli") # name as Separate string
print("Birthday: April 8, 1980") # Birthday as Separate String
if __name__ == "__main__": # Checking if this script is being run directly
main() # Calling the main function
[Unit II: Python Programming]
Program 4: Write a program that asks two people for their names, stores the names in variables name1 and name2,
and says hello to both of them.
Start
Algorithm: Say 'hello' to greet two people
Input: The first-person name.
1. Start
2. Input: The first-person name.
Store the first person's name
3. Store the first person's name in variable
in variable name1.
name1.
4. Input: The second-person name.
Input: The second-person name.
5. Store the second person's name in variable
name2.
6. Output: print Variable name1 with Store the Second person's
name in variable name2.
the greeting message: "Hello, name1!"
7. Output: print Variable name2 with
Output: print variable name1
the greeting message: "Hello, name2!" the greeting message: "Hello, name1!"
8. End
Output: print variable name2 with
the greeting message: "Hello, name2!"
Stop
Output of Program :
[Unit II: Python Programming]
Date
Page No.
Program 4: Write a program that asks two people for their names, stores the names in
variables name1 and name2, and says hello to both of them.
def main(): # Defining the main function
# name1 variable declaration
# Asking for the first person's name
name1 = input( "Enter the name of the first person: " )
# name2 variable declaration
# Asking for the second person's name
name2 = input("Enter the name of the second person: ")
# Saying hello to both people
print("Hello, " + name1 + "!")
print("Hello, " + name2 + "!")
if __name__ == "__main__": # Checking if this script is being run directly
main() # Calling the main function
[Unit II: Python Programming]
Program 5: Write a Python program prompts the user for their name and city, then displays a
personalized welcome message.
Start
Algorithm:
1. Start: Begin the program. Input: The person’s name.
2. Prompt for Name: Ask the user to
input their name and store the Store the person’s name
in variable name1.
input in the name1 variable.
3. Prompt for City: Ask the user to
Input: Enter the City:
input their city and store the input
in the name2 variable.
Store the city in variable name2.
4. Print Dotted Line: Print a series of
dashed lines for separation using
the print() function. Output: print dotted line
5. Print "Your details are:": Print the
message "Your details are:" to Output: print “Your Details are:”
introduce the user's information.
6. Display Personalized Welcome Output: print greeting message:
"Welcome” and name1 and from and name 2
Message: Concatenate the name1
and name2 variables into a
welcome message and print it in Stop
the format: "Welcome [name1]
from [name2]".
7. End: End the program.
[Unit II: Python Programming]
Date
Page No.
Program 5: Write a Python program prompts the user for their name and city, then displays a
personalized welcome message.
def main(): # Defining the main function
# name1 variable declaration
name1 = input("Enter Your Name:") # Asking for the person's name
# name2 variable declaration
name2 = input("Enter Your City: ") # Asking for the person's City
print("--------------------------------------------------") # add doted line to console
print("Your details are:") # print message
print("Welcome " + name1 + " from "+ name2) # print final Message with detail
if __name__ == "__main__": # Checking if this script is being run directly
main() # Calling the main function
[Unit II: Python Programming] Chapter-3 Data Types, Operators & Expressions in Python
Start
Algorithm: Perform Arithmetic Operations on Two
Integers
1. Start Input: enter the first integer.
2. Prompt the user to enter the first integer
→ Store it in num1
Store first integer in variable num1.
3. Prompt the user to enter the second
integer → Store it in num2
4. Perform the following arithmetic Input: enter the Second integer.
operations and display the results:
o Add num1 and num2, display the
result ("Addition") Store the Second Integer in variable num1.
o Subtract num2 from num1, display
the result ("Subtraction")
o Multiply num1 and num2, display Perform the following arithmetic
the result ("Multiplication") operations: Addition, Subtraction,
Multiplication, Division, Floor Division,
o Divide num1 by num2, display the Modulus and Exponentiation
result ("Division")
o Perform floor division (num1 //
Output: print arithmetic operations Result
num2), display the result ("Floor
Division")
o Find modulus (num1 % num2),
display the result ("Modulus")
Stop
o Perform exponentiation (num1 **
num2), display the result
("Exponentiation")
5. End
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Date
Page No.
Program 1: Write a program to enter two integers and perform all Arithmetic Operations on them.
def main():
# Input from the user
num1 = int(input("Enter first integer: "))
num2 = int(input("Enter second integer: "))
# Performing arithmetic operations
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
print("Floor Division:", num1 // num2)
print("Modulus:", num1 % num2)
print("Exponentiation:", num1 ** num2)
# Call the main function
if __name__ == "__main__":
main()
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Program 2: Write a Python program to Swap two numbers. Start
Input: enter the first integer.
Algorithm: Swap Two Numbers Without Store first integer in variable num1.
Using a Temporary Variable
1. Start
2. Prompt the user to enter the first number Input: enter the Second integer.
→ Store the value in variable num1
3. Prompt the user to enter the second
number
→ Store the value in variable num2 Store the Second Integer in variable num2.
4. Display the values of num1 and num2 before
swapping
5. Swap the values of num1 and num2 using
tuple unpacking:
num1, num2 = num2, num1 Output: print num1 and num2 before swapping
6. Display the values of num1 and num2 after
swapping
7. End
Swap the values of num1 and num2 using tuple
unpacking: num1, num2 = num2, num1
Output: print num1 and num2 after swapping
Stop
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Date
Page No.
Program 2: Write a Python program to Swap two numbers.
def main():
num1 = int(input("Enter first number: "))
num2 = int(input( "Enter second number: "))
print( "\nBefore Swapping:" )
print( "num1 =", num1)
print( "num2 =", num2)
# Swapping without temporary variable
num1, num2 = num2, num1
print( "\nAfter Swapping:" )
print( "num1 =", num1)
print( "num2 =", num2)
if __name__ == "__main__":
main()
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Program 3: Write a Python program that asks three values to find average of three numbers.
Start
Algorithm: Find the Average of Three Numbers
1. Start Input: enter the first integer.
2. Prompt the user to enter the first number
→ Store the value in num1
Store first integer in variable num1.
3. Prompt the user to enter the second number
→ Store the value in num2
4. Prompt the user to enter the third number Input: enter the Second integer.
→ Store the value in num3
5. Calculate the average using the formula:
average = (num1 + num2 + num3) / 3 Store the Second Integer in variable num2.
6. Display the result:
"The average of the three numbers is: average"
Input: enter the Third integer.
7. End
Store the Third Integer in variable num3.
Calculate the average using the formula:
average = (num1 + num2 + num3) / 3
Output: print result of average of three number
arithmetic operations Result
Stop
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Date
Page No.
Program 3: Write a Python program that asks three values to find average of three numbers.
def main():
# Input three numbers from the user
num1 = float(input( "Enter first number: " ))
num2 = float(input( "Enter second number: " ))
num3 = float(input( "Enter third number: " ))
# Calculate average
average = (num1 + num2 + num3) / 3
# Display the result
print( "The average of the three numbers is:" , average)
# Call the main function
if __name__ == "__main__":
main()
Chapter-3 Data Types, Operators & Expressions in Python Start
[Unit II: Python Programming]
Program 4: Write a Program that asks radius (r) to find the area of circle.
Import the Math package
Input: enter the radius.
Algorithm: Find the Area of a Circle
1. Start
Store the value in variable2
2. Prompt the user to enter the
radius of the circle
→ Store the value in variable r
3. Calculate the area of the circle Calculate the area of the circle using the
using the formula: formula: area = π × r × r
area = π × r × r
(Use π ≈ 3.14159 or use the
math.pi constant)
Output: print The area of the circle is: area
4. Display the result:
"The area of the circle is: area"
5. End Stop
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Date
Page No.
Program 4: Write a Program that asks radius (r) to find the area of circle.
import math
def main():
# Input: Ask user to enter the radius
r = float(input( "Enter the radius of the circle: " ))
# Process: Calculate area using the formula: πr²
area = math.pi * r * r
# Output: Display the area
print("The area of the circle is:", area)
# Call the main function
if __name__ == "__main__":
main()
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Program 5: Write a Program that asks the individual marks of subjects to calculate the percentage of marks
of your 10th class result.
Start
Algorithm
1. Start Output: "Enter marks..."
2. Display "Enter the marks obtained in each
subject:" Input: "Input marks for Punjabi,
3. Input marks for: English, Hindi, Math, Science & Social
Science"
o Punjabi (out of 150)
o English (out of 100)
Store the input marks in Variables
o Hindi (out of 100) and Calculate the total marks and
store in variable
o Math (out of 100)
o Science (out of 100)
Set total Max marks to 650 and calculate
o Social Science (out of 100) the Percentage
4. Calculate total_marks_obtained = sum of Calculate percentage =
all marks (total_marks_obtained / total_max_marks)
× 100
5. Set total_max_marks = 650
6. Calculate percentage =
(total_marks_obtained / Output: print Total marks obtained &
total_max_marks) × 100 Percentage
7. Display:
o Total marks obtained
Stop
o Percentage
8. End
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Date
Page No.
Program 5: Write a Program that asks the individual marks of subjects to calculate the percentage of marks
of your 10th class result.
def main():
print( "Enter the marks obtained in each subject:" )
# Input marks for each subject
punjabi = float(input("Punjabi (out of 150): "))
english = float(input( "English (out of 100): " ))
hindi = float(input( "Hindi (out of 100): " ))
math = float(input( "Math (out of 100): " ))
science = float(input( "Science (out of 100): " ))
social_science = float(input( "Social Science (out of 100): " ))
# Total and percentage calculation
total_marks_obtained = punjabi + english + hindi + math + science + social_science
total_max_marks = 650
percentage = (total_marks_obtained / total_max_marks) * 100
# Display result
print( "\n--- Result ---" )
print( f"Total Marks Obtained: {total_marks_obtained} / {total_max_marks}" )
print( f"Percentage: {percentage:.2f}%")
# Call the main function
If __name__ == "__main__":
main()
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Program 6: Write a program that asks for the temperature in degrees Celsius and converts it into Fahrenheit
using the formula: F = C × 9/5 + 32.
Start
Algorithm: Convert Celsius to Fahrenheit Output: "Temperature
Converter: Celsius to Fahrenheit"
1. Start
2. Display the message: "Temperature Converter:
Input: " enter temperature in degrees Celsius "
Celsius to Fahrenheit"
3. Prompt the user to enter temperature in
degrees Celsius Store the input temperature in
Variables Celsius
4. Read and store the input in variable Celsius
5. Apply the conversion formula:
Fahrenheit = (Celsius × 9/5) + 32 Apply the conversion formula:
Fahrenheit = (Celsius × 9/5) + 32
6. Display the result:
"<Celsius>°C is equal to <Fahrenheit>°F"
7. End
Output: print "<Celsius>°C is equal to
<Fahrenheit>°F"
Stop
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Date
Page No.
Program 6: Write a program that asks for the temperature in degrees Celsius and converts it into
Fahrenheit using the formula: F = C × 9/5 + 32.
def main():
print( "Temperature Converter: Celsius to Fahrenheit" )
# Ask the user to enter temperature in degrees Celsius
celsius = float(input( "Enter temperature in degrees Celsius: " ))
# Convert to Fahrenheit using the formula
fahrenheit = (celsius * 9/5) + 32
# Display the result
print( f"{celsius}°C is equal to {fahrenheit:.2f}°F" )
# Run the main function
if __name__ == "__main__":
main()
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Program 7: Write a program to calculate 10% discount on the total price of sold items.
Start
Algorithm: Calculate 10% Discount on
Total Price
Output: "Discount Calculator -
1. Start 10% Off"
2. Display the message: "Discount Calculator
- 10% Off"
3. Prompt the user to enter the total price of Input: enter the total price of sold items
sold items
4. Read and store the input in a variable called
total_price
5. Calculate the discount using the formula: Store the input total price in Variables Celsius
discount = total_price × 10 / 100
6. Calculate the final price after discount:
final_price = total_price - discount
7. Display: Calculate the discount using the formula:
o Original Price
discount = total_price × 10 / 100
o Discount Amount
Calculate the final price after discount:
o Final Price to be paid
final_price = total_price - discount
8. End
Output: print Original Price, Discount
Amount , Final Price to be paid
Stop
Chapter-3 Data Types, Operators & Expressions in Python
[Unit II: Python Programming]
Date
Page No.
Program 7: Write a program to calculate 10% discount on the total price of sold items.
def main():
print( "Discount Calculator - 10% Off" )
# Ask the user to enter the total price of sold items
total_price = float(input( "Enter the total price of sold items: " ))
# Calculate 10% discount
discount = total_price * 10 / 100
# Calculate final price after discount
final_price = total_price - discount
# Display results
print( f"\nOriginal Price: ₹{total_price:.2f}" )
print( f"Discount (10%): ₹{discount:.2f}" )
print( f"Final Price to be paid: ₹{final_price:.2f}" )
# Run the main function
if __name__ == "__main__":
main()