Python Phase 1 Basics
Python Phase 1 Basics
Python Basics
Introduction
Phase 1 is designed for absolute beginners who want to build a solid foundation in Python. It
covers basic concepts like variables, data types, operators, loops, functions, and more.
Example:
name = "John"
age = 20
is_student = True
Lesson 2: Operators
Operators are used to perform operations on variables and values.
Types of operators:
- Arithmetic: +, -, *, /, %, **, //
- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
- Assignment: =, +=, -=, *=, etc.
Syntax:
if condition:
# do something
elif condition:
# do something else
else:
# fallback
Lesson 4: Loops
Loops are used to repeat a block of code multiple times.
Example:
for i in range(5):
print(i)
Lesson 5: Functions
A function is a block of reusable code that performs a specific task.
Syntax:
def function_name(parameters):
# code
return value
Example:
def greet(name):
return "Hello, " + name
Example:
text = " Hello World "
print(text.strip().upper())
Lesson 8: Basic Input/Output
Use input() to get user input, and print() to display output.
Example:
name = input("Enter your name: ")
print("Hello", name)
Syntax:
try:
# risky code
except ExceptionType:
# handle error
Example:
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input")