Python Practice Notebook: Chapter – Variables, Expressions and Statements¶

Practice:¶

  • Variable declaration
  • Expressions using arithmetic operators
  • Printing results
  • Simple input/output
  • Type conversion (int, float, str)

1. Creating and Printing Variables¶

In [70]:
# Define a variable and print it
x = 5
print("The value of x is:", x)
The value of x is: 5
In [71]:
x = 6
y = x+6**2
print("The value of x/y is:",round(x/y,3))
The value of x/y is: 0.143

2. Changing Values and Using Arithmetic Operators¶

In [22]:
x = 10
y = x + 3
print("x + 3 =", y)

# Try subtraction, multiplication, division
print("x - 2 =", x - 2)
print("x * 3 =", x * 3)
print("x / 2 =", x / 2)
x + 3 = 13
x - 2 = 8
x * 3 = 30
x / 2 = 5.0

3. Combining Strings with Variables¶

In [27]:
name = "Lara"
print("Hello,", name)
Hello, Lara
In [26]:
name = "Tom"
print("Welcome,",name)
Welcome, Tom
In [30]:
name = "Aliya"
subject = "Python programming"
print(f"Hello, {name}. Welcome to {subject}!")
Hello, Aliya. Welcome to Python programming!

4. Taking Input from User¶

In [32]:
# input always returns a string
name = input("What is your name? ")
print("Nice to meet you,", name)
Nice to meet you, lara
In [45]:
name = input("What is your name?")
score = input("Enter your score,Here:")
subject = input("What is your subject?")
print(f"{name}, you scored {score} in {subject}. well done!") 
Lara, you scored 98 in Python Programming. well done!

5. Doing Math with Input¶

In [55]:
# Convert input string to number
num = float(input(("Enter a number: ")))
num = int(num)  # Convert to integer
print("Double the number is:", num * 2)
Double the number is: 4
In [58]:
num = float(input("Enter a number:"))
num = round(num)
print("Square of the number is:" , num**2)
Square of the number is: 9

6. Float Input and Arithmetic¶

In [8]:
height = float(input("Enter your height in meters: "))
print("If you grew 10%, your new height would be:", height * 1.1)
If you grew 10%, your new height would be: 5.5

7. Mini Project: Calculate Total Bill¶

In [9]:
# Total cost calculator
price = float(input("Enter price of one item: "))
qty = int(input("How many items? "))
total = price * qty
print("Your total bill is:", total)
Your total bill is: 250.0

Bonus Practice (Try Yourself)¶

In [59]:
a = 7 
b = a*2
print("Value of b is:" , b)
print("Value of a+b is:",a+b)
Value of b is: 14
Value of a+b is: 21
In [68]:
Birthyear = float(input("What is your birth year?"))
Currentyear = 2025
Age = Currentyear - Birthyear
print("Your age is:", Age)
Your age is: 19.0
In [69]:
Length = float(input("Enter length of rectangle:"))
Width = float(input("Enter width of rectangle:"))
Area = Length * Width
print("Area of rectangle will be:" ,Area)
Area of rectangle will be: 10.0