Python Syntax & Basics
Master Python's clean and readable syntax, learn about variables, basic operations, and writing your first Python programs.
Python Syntax Overview
Python's syntax is designed to be readable and concise. Here are the fundamental concepts:
🎯 Python Philosophy
"Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex."
- The Zen of Python
Variables & Basic Operations
Variables and Assignment
# Variable assignment (no declaration needed)
name = "Alice"
age = 25
height = 5.6
is_student = True
# Multiple assignment
x, y, z = 1, 2, 3
# Swapping variables
a, b = b, a
print(f"Name: {name}, Age: {age}, Height: {height}")
# Output: Name: Alice, Age: 25, Height: 5.6
Basic Operations
# Arithmetic operations
result = 10 + 5 * 2 # 20 (follows order of operations)
power = 2 ** 3 # 8 (exponentiation)
division = 10 / 3 # 3.333... (float division)
int_division = 10 // 3 # 3 (integer division)
remainder = 10 % 3 # 1 (modulo)
# String operations
greeting = "Hello" + " " + "World" # "Hello World"
repeated = "Python" * 3 # "PythonPythonPython"
# Comparison operations
is_equal = (5 == 5) # True
is_greater = (10 > 5) # True
is_different = (1 != 2) # True
Data Types
📊 Numeric Types
# Integers
count = 42
negative = -17
# Floats
price = 19.99
scientific = 1.5e6 # 1,500,000
# Complex numbers
complex_num = 3 + 4j
📝 Strings
# String creation
single = 'Hello'
double = "World"
multiline = """This is a
multi-line string"""
# String methods
text = "python programming"
print(text.upper()) # PYTHON PROGRAMMING
print(text.title()) # Python Programming
print(text.split()) # ['python', 'programming']
Your First Python Program
🚀 Try This Exercise
# Create a simple calculator
def calculator():
print("Simple Python Calculator")
# Get user input
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
# Perform calculation
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 != 0:
result = num1 / num2
else:
return "Error: Division by zero!"
else:
return "Error: Invalid operator!"
return f"{num1} {operator} {num2} = {result}"
# Run the calculator
print(calculator())