Python Programming – Day 1
Example 1 – Display Simple Messages
print("Hello World..")
print("Welcome to Python Programming")
print("Hi Lahiru")
Example 2 – Display a Message Using a Variable
# Write a Python program that prints a welcome message using a variable
message = "Welcome to Python programming.."
print(message)
Note: Variables store data while the program is running.
Example 3 – Display Your Name and Age
# Display your name and age using 2 variables
name = "Lahiru"
age = "36"
print("I'm", name, "and", age, "years old")
Example 4 – Add Two Numbers (Using int() function)
x = int(input("Enter 1st Number: "))
y = int(input("Enter 2nd Number: "))
z=x+y
print("The sum is:", z)
input() – waits for user input
print() – displays output
int() – converts a string value to an integer
Example 5 – Add Two Numbers (Without int() function)
# Remove int() function
x = input("Enter 1st Number: ")
y = input("Enter 2nd Number: ")
z=x+y
print("The result is:", z)
Note: Without int(), inputs are treated as text and will be joined together instead of added
numerically.
Example 6 – Calculate Age
# Create a program that takes user's birth year and calculates their age
birthyear = int(input("Enter your birth year: "))
currentyear = int(input("Enter current year: "))
age = currentyear - birthyear
print("My age is:", age)
Example 7 – Area of a Rectangle
# Calculate the area of a rectangle using user input
# Formula: area = length * width
length = int(input("Enter the length: "))
width = int(input("Enter the width: "))
area = length * width
print("The area of the rectangle is:", area)
Complete the Following Tasks
1. Write a program that asks for two numbers and displays their sum.
2. Convert the string '100' into an integer and multiply it by 2.
3. Write a program that adds three numbers and displays the total.
4. Write a program that checks if a number is Even or Odd.
5. Answer: What is the difference between int and float?
6. List and describe four different data types in Python.
Tips
• Always test your programs with different inputs.
• Use comments (#) to explain what your code does.
• Practice regularly to improve your understanding of Python.
Prepared by: Lahiru Neththasinghe