Key elements of Python syntax:
Variables and Data Types
Variables in Python are dynamically typed, meaning you don’t need to declare their type. You can directly assign values to variables.
# Integer
x = 5
# Float
y = 3.14
# String
name = "Alice"
# Boolean
is_active = True
PythonComments
Comments are used to explain the code and are ignored by the interpreter. Single-line comments start with #
.
# This is a single-line comment
'''
This is a
multi-line comment
'''
PythonIndentation
Python uses indentation to define blocks of code. Indentation is usually 4 spaces.
if x > 0:
print("x is positive")
else:
print("x is zero or negative")
PythonFunctions
Functions are defined using the def
keyword.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
PythonControl Flow
Python supports various control flow statements like if
, for
, and while
.
If Statements
if x > 0:
print("x is positive")
elif x == 0:
print("x is zero")
else:
print("x is negative")
PythonFor Loops
for i in range(5): # 0 to 4
print(i)
PythonWhile Loops
i = 0
while i < 5:
print(i)
i += 1
PythonData Structures
Python has several built-in data structures, including lists, tuples, sets, and dictionaries.
Lists
Lists are ordered, mutable collections.
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']
PythonTuples
Tuples are ordered, immutable collections.
coordinates = (10, 20)
print(coordinates[0]) # Output: 10
PythonSets
Sets are unordered collections of unique elements.
unique_numbers = {1, 2, 3, 3, 4}
print(unique_numbers) # Output: {1, 2, 3, 4}
PythonDictionaries
Dictionaries are collections of key-value pairs.
person = {"name": "Alice", "age": 25}
print(person["name"]) # Output: Alice
PythonList Comprehensions
List comprehensions provide a concise way to create lists.
squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
PythonError Handling
Python uses try
, except
, else
, and finally
to handle exceptions.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Division successful")
finally:
print("Execution complete")
PythonClasses and Objects
Python supports object-oriented programming (OOP) with classes and objects.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says woof!"
dog = Dog("Buddy")
print(dog.bark()) # Output: Buddy says woof!
PythonModules and Packages
Python code can be organized into modules and packages. Modules are individual files, and packages are collections of modules.
Importing a Module
import math
print(math.sqrt(16)) # Output: 4.0
PythonImporting Specific Functions
from math import sqrt
print(sqrt(16)) # Output: 4.0
PythonCreating a Module
Create a file named mymodule.py
:
def greet(name):
return f"Hello, {name}!"
PythonThen, import and use it:
import mymodule
print(mymodule.greet("Alice")) # Output: Hello, Alice!
PythonThese are some fundamental concepts and syntax elements in Python. The language is designed to be easy to learn and use, with a strong emphasis on readability and simplicity.