Student Login
Python Programming

Your First Python Program

If Statements: Making Decisions

  • Programs need to make decisions based on conditions:
  • “`python
  • age = 20
  • if age >= 18:
  • print('You are an adult')
  • else:
  • print('You are a minor')
  • “`
  • Important: Python uses indentation (4 spaces) instead of brackets!

Elif: Multiple Conditions

  • When you have more than two options:
  • “`python
  • marks = 85
  • if marks >= 90:
  • print('Grade: A+')
  • elif marks >= 80:
  • print('Grade: A')
  • elif marks >= 70:
  • print('Grade: B')
  • else:
  • print('Grade: C')
  • “`

For Loops: Repeating Tasks

  • Repeat actions a specific number of times:
  • “`python
  • for i in range(5):
  • print('Hello', i)
  • “`
  • This prints 'Hello' 5 times (0 to 4).
  • Loop through a list:
  • “`python
  • fruits = ['apple', 'banana', 'mango']
  • for fruit in fruits:
  • print(fruit)
  • “`

While Loops: Repeat Until Condition

  • Keep repeating as long as a condition is true:
  • “`python
  • count = 1
  • while count <= 5:
  • print(count)
  • count = count + 1
  • “`
  • This prints numbers 1 to 5.

Real Example: Calculator Program

  • “`python
  • num1 = float(input('Enter first number: '))
  • num2 = float(input('Enter second number: '))
  • operation = input('Enter operation (+, -, *, /): ')
  • if operation == '+':
  • result = num1 + num2
  • elif operation == '-':
  • result = num1 – num2
  • elif operation == '*':
  • result = num1 * num2
  • elif operation == '/':
  • result = num1 / num2
  • else:
  • result = 'Invalid operation'
  • print('Result:', result)
  • “`
⬅️ Previous Lesson Python Basics Next Lesson ➡️ Data Structures
Scroll to Top