Print statement:
The print() function is used to display output on the console.
Syntax: print("Hello, World!")
Variables:
Variables are used to store data in memory. In Python, you don't need to declare the type of variable before using it.
Syntax: variable_name = value
Example: name = "John"
Data types:
Python supports various data types, including integer, float, string, boolean, list, tuple, set, and dictionary.
Syntax: variable_name = value
Example: age = 30
Conditional statements:
Conditional statements are used to execute different blocks of code based on certain conditions.
Syntax:
if condition:
# code to execute if condition is true
elif condition:
# code to execute if the first condition is false and this condition is true
else:
# code to execute if both conditions are false
Loops:
Loops are used to execute a block of code repeatedly.
a. For loop:
for variable in sequence:
# code to execute for each value in the sequence
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
b. While loop:
while condition:
# code to execute as long as the condition is true
Example:
i = 0
while i < 5:
print(i)
i += 1
Functions:
Functions are used to group a set of related statements and reuse them throughout the program.
Syntax:
def function_name(parameters):
# code to execute
return value
Lists:
Lists are used to store multiple items in a single variable. They are mutable, meaning their values can be changed.
Syntax: my_list = [item1, item2, item3]
Example: fruits = ["apple", "banana", "cherry"]
Tuples:
Tuples are used to store multiple items in a single variable. They are immutable, meaning their values cannot be changed.
Syntax: my_tuple = (item1, item2, item3)
Example: fruits = ("apple", "banana", "cherry")
Dictionaries:
Dictionaries are used to store key-value pairs. They are mutable, meaning their values can be changed.
Syntax: my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
Example: person = {"name": "John", "age": 30, "city": "New York"}
Modules:
Modules are used to organize code into reusable files. You can import modules in your program to use their functions and variables.
Syntax: import module_name
Example: import math
File I/O:
File I/O is used to read from and write to files on the computer.
a. Reading from a file:
with open("filename.txt", "r") as f:
contents = f.read()
Example:
open("test.txt", "r") as f:
contents = f.read()
print(contents)
b. Writing to a file:
with open("filename.txt", "w") as f:
f.write("contents to write")
Exception handling:
Exception handling is used to catch and handle errors in a program.
Syntax:
try:
# code that might raise an exception
except ExceptionType:
# code to handle the exception
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Classes and objects:
Classes and objects are used in object-oriented programming to define data structures and their behavior.
Syntax:
class ClassName:
def __init__(self, parameters):
# code to initialize the object
def method_name(self, parameters):
# code to define a method
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print("Hello, my name is " + self.name)
john = Person("John", 30)
john.say_hello()
Built-in functions:
Python has many built-in functions that can be used to perform common tasks.
Syntax: function_name(parameters)
Example: print("Hello, World!")
List comprehension:
List comprehension is a concise way to create a list based on an existing list.
Syntax: [expression for item in list if condition]
Example: squares = [x*x for x in range(1, 11)]
Lambda functions:
Lambda functions are used to create small, anonymous functions. They are often used as arguments to higher-order functions.
Syntax: lambda parameters: expression
Example: square = lambda x: x*x
Decorators:
Decorators are used to modify the behavior of functions or classes.
Syntax:
@decorator_function
def my_function(parameters):
# code to define the function
Example:
def my_decorator(function):
def wrapper(*args, **kwargs):
# code to modify the function
return function(*args, **kwargs)
return wrapper
@my_decorator
def my_function(parameters):
# code to define the function
Generators:
Generators are used to create iterators. They are useful when you need to iterate over a large dataset that cannot fit into memory.
Syntax:
def my_generator():
# code to generate the data
yield data
Example:
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
Regular expressions:
Regular expressions are used to search for and manipulate text patterns.
Syntax: re.search
(pattern, string)
Example: re.search
("[a-z]+", "Hello, World!")
Concurrency:
Concurrency is used to run multiple tasks at the same time.
Syntax:
pythonCopy codeimport threading
def my_function(parameters):
# code to define the function
t1 = threading.Thread(target=my_function, args=(arguments,))
t1.start()
Example:
import threading
def print_numbers():
for i in range(1, 11):
print(i)
def print_letters():
for letter in "abcdefghij":
print(letter)
t1 = threading.Thread(target=print_numbers)
t2 = threading.Thread(target=print_letters)
t1.start()
t2.start()