Python keywords are reserved words with fixed meanings that define Python's syntax. They cannot be used as variable names, function names, or any other identifiers.
Fun Fact
All keywords in Python are in lowercase except True, False, and None!
Keyword Categories
Category
Keywords
Value Keywords
True, False, None
Operator Keywords
and, or, not, is, in
Control Flow
if, else, elif, for, while, break, continue, pass
Exception Handling
try, except, finally, raise, assert
Function & Class
def, return, lambda, yield, class
Import & Module
import, from, as
Scope Management
global, nonlocal
Async Programming
async, await
View All Keywords
You can programmatically get all Python keywords:
import keyword
print(keyword.kwlist)
Python Identifiers
What are Identifiers?
Identifiers are user-defined names for variables, functions, classes, modules, etc. They help us identify and reference different elements in our code.
Rules for Naming Identifiers
✅ Can contain letters (A-Z, a-z), digits (0-9), and underscores (_)
✅ Must start with a letter or underscore (not a digit)
✅ Cannot be a reserved Python keyword
✅ Cannot contain spaces or special characters
✅ Case sensitive (name, Name, and NAME are different)
Valid vs Invalid Identifiers
✅ Valid Identifiers
name
_count
user_name
User1
calculate_sum
❌ Invalid Identifiers
2var (starts with digit)
my-name (contains hyphen)
class (keyword)
user@name (special char)
var name (contains space)
Best Practices
🎯 Use descriptive names (student_name vs s)
🐍 Follow Python conventions (snake_case for variables)
📏 Keep names reasonable in length
Input and Output Operations
Taking User Input
The input() function reads user input from the keyboard:
# Basic input
name = input("Enter your name: ")
print("Hello, " + name + "!")
Try It Yourself!
Output will appear here...
Type Conversion
By default, input() returns a string. Convert to other types:
# Convert to integer
age = int(input("Enter your age: "))
# Convert to float
price = float(input("Enter price: "))
# Convert to boolean
is_student = input("Are you a student? (y/n): ") == 'y'
Multiple Inputs
Take multiple values in one line using split():
# Take two inputs
x, y = input("Enter two numbers: ").split()
print("First:", x, "Second:", y)
# Convert to integers
a, b = map(int, input("Enter two numbers: ").split())
Advanced Output Formatting
# f-strings (Python 3.6+)
name = "Alice"
age = 25print(f"{name} is {age} years old.")
# format() methodprint("Hello, {}. You are {}.".format(name, age))
# String formatting with specifiers
pi = 3.14159print("Pi = {:.2f}".format(pi))
Practical Examples
Example 1: Logical Operators
# Using and, or, not operatorsprint(TrueandTrue) # Trueprint(TrueorFalse) # Trueprint(notFalse) # True
for i inrange(1, 11):
if i == 5:
continue# Skip 5if i == 8:
break# Stop at 8print(i)
Example 4: Function Definition
defcalculate_area(length, width):
"""Calculate rectangle area"""return length * width
# Call the function
area = calculate_area(5, 3)
print(f"Area: {area}")
Example 5: Exception Handling
defsafe_divide(a, b):
try:
result = a / b
return result
except ZeroDivisionError:
return"Cannot divide by zero!"finally:
print("Division attempt completed")
print(safe_divide(10, 2))
print(safe_divide(10, 0))
Test Your Knowledge
Question 1: Which of these is NOT a Python keyword?
if
variable
for
while
Question 2: Which identifier is valid?
2name
my-name
_count
class
Question 3: What does input() return by default?
Integer
String
Float
Boolean
Question 4: Which is the correct way to convert input to integer?