Python Basics
Introduction
Learn the fundamental concepts of Python programming.
Python is a high-level, interpreted language with dynamic typing and automatic memory management. It supports multiple paradigms, including procedural, object-oriented, and functional programming.
print("Hello, World!")Python Syntax
Python uses indentation to define blocks instead of braces. Comments start with # and can be single-line or multi-line inside triple quotes.
- Indentation: 4 spaces per level is standard.
- Comments:
# This is a comment. - Multi-line comments:
"""This is a docstring""".
def greet(name):
print(f"Hello, {name}")
Variables & Data Types
Variables are dynamically typed. Common data types:
- int: integers, e.g.,
x = 10 - float: floats, e.g.,
pi = 3.14 - str: strings, e.g.,
name = "Python" - bool: booleans,
TrueorFalse - list, tuple, dict, set
count = 5
items = [1, 2, 3]
config = {"debug": True}
Control Flow
Control flows guide program execution:
# if statement
if x > 0:
print("Positive")
else:
print("Non-positive")
# for loop
for i in range(5):
print(i)
# while loop
while condition:
do_something()
Functions
Functions are defined with def and can have default or keyword arguments:
def add(a, b=0):
"""Return the sum of a and b."""
return a + b
# lambda expression
square = lambda x: x * x
print(square(5))