Basic Syntax
Variables and Types
x = 42 # variable declaration with initialization
x = "hello" # variable of string type
x = True # variable of boolean type
x = 3.14 # variable of float type
x = [1, 2, 3] # variable of list type
Functions
def name(arg1, arg2):
# function body
return value
def add(a, b):
return a + b
Control Structures
if condition:
# code to be executed if condition is true
elif condition2:
# code to be executed if condition2 is true
else:
# code to be executed if all conditions are false
for i in range(10):
# code to be executed repeatedly
while condition:
# code to be executed repeatedly while condition is true
Pointers
Python does not have pointers in the traditional sense. Instead, all variables are references to objects in memory.
Packages
import math # import package named math
def main():
print("The value of pi is", math.pi) # use value from math package
Lists
a = [1, 2, 3] # declare a list
a.append(4) # add an element to the end of the list
a.extend([5, 6, 7]) # add multiple elements to the end of the list
a = a[1:3] # slice the list to get a new list [2, 3]
Dictionaries
person = {"name": "John", "age": 30} # declare a dictionary
person["age"] = 31 # change the value of age
Classes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("John", 30) # create a new Person object
p.age = 31 # change the value of age
Inheritance
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
s = Student("Jane", 20, 12345) # create a new Student object
Interfaces
Python does not have interfaces in the traditional sense. Instead, any object that has a certain set of methods can be used as if it implements an interface.
This is just a brief overview of Python syntax and features. For a more comprehensive guide, please refer to the official Python documentation.