Identifiers are names used to identify variables, functions, classes, modules, etc.
- Must start with a letter (A-Z, a-z) or an underscore (_)
- Can contain letters, digits, and underscores
- Cannot use keywords like
for
,if
,else
,class
, etc. - Are case-sensitive:
Name
andname
are different.
Operators are symbols that perform operations on variables and values.
Type | Operators | Example |
---|---|---|
Arithmetic | + , - , * , / , // , % , ** |
a + b |
Comparison | == , != , > , < , >= , <= |
a > b |
Logical | and , or , not |
a > b and b > c |
Assignment | = , += , -= , *= , /= , etc. |
x += 2 |
Bitwise | & , ` |
, ^, ~, <<, >>` |
Membership | in , not in |
'a' in 'apple' |
Identity | is , is not |
a is b |
Variables are containers for storing data values.
Example:
name = "Guru"
age = 25
- Must start with a letter or underscore
- Cannot start with a digit
- Can contain letters, numbers, and underscores
- Cannot be a keyword
- Are case-sensitive
Python has the following built-in data types:
Type | Example | Description |
---|---|---|
int | x = 10 |
Integer |
float | y = 5.5 |
Decimal (floating point) |
bool | True , False |
Boolean |
str | "hello" |
String |
list | fruits = ["apple", "banana"] |
Mutable ordered collection |
tuple | days = ("Mon", "Tue", "Wed") |
Immutable ordered collection |
set | nums = {1, 2, 3} |
Unordered unique elements |
dict | person = {"name": "Guru", "age": 25} |
Key-value pairs |
complex | z = 2 + 3j |
Complex numbers |
range | range(5) |
Sequence from 0 to 4 |
-
List: Mutable, ordered
Example:fruits = ["apple", "banana"]
-
Tuple: Immutable, ordered
Example:days = ("Mon", "Tue", "Wed")
-
Set: Mutable, unordered, unique elements
Example:nums = {1, 2, 2, 3}
-
Dictionary: Key-value pairs
Example:person = {"name": "Guru", "age": 25}
-
String: Text data
Example:name = "Python"
-
Range: Generates sequence of numbers
Example:for i in range(5): print(i)
# Python Basics - Conditional Statements, Loops, and Functions
## 5. Conditional Statements (`if`, `elif`, `else`)
Used to perform different actions based on conditions.
### Example:
```python
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Used to iterate over sequences like lists, strings, etc.
for i in range(3):
print(i)
Runs as long as the condition is True.
Example:
i = 0
while i < 3:
print(i)
i += 1
Predefined functions provided by Python.
Examples: print()
, len()
, type()
, input()
Functions created by users using the def
keyword.
def greet(name):
return "Hello " + name
print(greet("Guru")) # Output: Hello Guru
A def
function is a normal function created using the def
keyword. It can take arguments, perform tasks, and return a value.
def function_name(arguments):
# function body
return result
Example of def function:
def greet(name):
return "Hello, " + name
print(greet("Guru")) # Output: Hello, Guru
A lambda function is an anonymous function defined using the lambda
keyword. It can have any number of arguments but only one expression. Lambda functions are commonly used for short-term operations or as arguments to higher-order functions like map()
, filter()
, and reduce()
.
lambda arguments: expression
multiply = lambda x, y: x * y
print(multiply(3, 5)) # Output: 15
A global variable is a variable that is defined outside of any function. It can be accessed from anywhere in the program, both inside and outside functions.
name = "Guru" # Global variable
def greet():
print("Hello, " + name) # Accessing global variable
greet() # Output: Hello, Guru
A local variable is a variable that is defined inside a function. It is only accessible within that function and cannot be accessed outside of it.
def greet():
name = "Guru" # Local variable
print("Hello, " + name)
greet() # Output: Hello, Guru
# print(name) # This will give an error because `name` is a local variable
Function arguments are values passed to a function when it is called. They are used to pass information into the function for processing.
def greet(name, age):
print(f"Hello, {name}! You are {age} years old.")
greet("Guru", 25) # Output: Hello, Guru! You are 25 years old.
Functions can return a value to the caller using the return
keyword. The returned value can be assigned to a variable, printed, or used in any other expression.
def add(a, b):
return a + b
result = add(3, 5)
print(result) # Output: 8
In Object-Oriented Programming (OOP), functions are typically defined inside classes and are known as methods. These methods operate on data contained within the object and can perform actions related to that object.
- Class: A blueprint for creating objects (a collection of attributes and methods).
- Object: An instance of a class.
- Inheritance: The ability to create a new class based on an existing class.
- Polymorphism: The ability to use a method in different ways.
- Abstraction: Hiding the complexity of the system and showing only the necessary parts.
- Encapsulation: Binding the data (attributes) and methods together inside a class and restricting access to them.
class Animal:
def __init__(self, name, sound):
self.name = name # Attribute
self.sound = sound # Attribute
def make_sound(self): # Method (OOP function)
return f"The {self.name} says {self.sound}"
# Create an object (instance) of the class Animal
dog = Animal("Dog", "Bark")
cat = Animal("Cat", "Meow")
print(dog.make_sound()) # Output: The Dog says Bark
print(cat.make_sound()) # Output: The Cat says Meow
File handling is an essential aspect of Python programming. Python provides several built-in functions and methods to work with files, including reading, writing, and manipulating files. You can open, read, write, and close files using Python's built-in functions.
Python uses different modes to open files. Here are the most commonly used modes:
"r"
: Read mode (default mode) – Opens the file for reading."w"
: Write mode – Opens the file for writing. If the file doesn't exist, a new file is created. If the file exists, it truncates the file."a"
: Append mode – Opens the file for writing. If the file doesn't exist, a new file is created. It appends data to the end of the file if the file already exists."b"
: Binary mode – Opens the file in binary mode (useful for non-text files like images).
Once a file is opened in read mode ("r"
), you can read its contents using methods like read()
, readline()
, and readlines()
.
# Open the file in read mode
file = open("example.txt", "r")
# Read the entire content of the file
content = file.read()
print(content)
# Close the file
file.close()
Exception handling is a mechanism in Python that allows us to deal with runtime errors, preventing the program from crashing. Python provides a set of keywords to handle exceptions: try
, except
, finally
, and else
.
The try
block is used to define a block of code that might raise an exception. If an exception occurs, Python stops executing the code in the try
block and moves to the except
block.
The except
block catches the exception raised in the try
block. You can specify the type of exception you want to catch or catch any exception in general.
The finally
block contains code that will be executed no matter what, whether an exception occurs or not. It is commonly used to perform cleanup operations, such as closing files or releasing resources.
The else
block is optional. It will execute only if no exception occurs in the try
block. It is used when you want to perform an action only if the try
block runs successfully without exceptions.
try:
# Code that might raise an exception
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
# Code to handle division by zero
print("Error: Cannot divide by zero!")
except ValueError:
# Code to handle invalid input
print("Error: Invalid input. Please enter a number.")
else:
# Code to execute if no exceptions occur
print("Result is:", result)
finally:
# Code that will always execute
print("Execution completed.")
Recursion is a programming concept where a function calls itself in order to solve a problem. This technique is often used to solve problems that can be broken down into smaller, similar sub-problems.
In a recursive function, there are typically two main components:
- Base Case: The condition that stops the recursion. Without a base case, the recursion will continue indefinitely.
- Recursive Case: The part of the function where it calls itself with modified arguments.
Let's take an example of calculating the factorial of a number using recursion.
Factorial of a number n
is defined as:
n! = n * (n-1) * (n-2) * ... * 1
- The base case is
0! = 1
.
Here's a Python example of how recursion works for calculating a factorial:
def factorial(n):
# Base case
if n == 0 or n == 1:
return 1
else:
# Recursive case
return n * factorial(n - 1)
# Test the function
result = factorial(5)
print(result) # Output: 120
A module in Python is a file containing Python code that can define functions, classes, and variables. It can also include runnable code. Modules allow you to break down large programs into smaller, manageable, and organized files.
You can import a module into your Python script using the import
keyword. Once imported, you can access the functions and variables defined within the module.
# Import the math module
import math
# Use the sqrt function from the math module
result = math.sqrt(16)
print(result) # Output: 4.0
To create a module, simply save a Python file with the .py
extension, and you can import it into other programs.
If you have a file my_module.py
with the following content:
# my_module.py
def greet(name):
return "Hello, " + name
A package is a collection of modules organized in directories. A package typically contains multiple modules (Python files) grouped together under a common directory.
To create a package, organize the Python files (modules) in a directory and include an __init__.py
file to mark the directory as a package. The __init__.py
file can be empty or contain initialization code for the package.
my_package/
__init__.py
module1.py
module2.py
my_package/
is the main package directory.__init__.py
is the file that tells Python to treat this directory as a package.module1.py
andmodule2.py
are modules within the package.
To import and use a module from a package:
from my_package import module1
module1.some_function()
A library is a collection of pre-written code (modules and packages) that you can use in your program to avoid reinventing the wheel. Python has a rich standard library, but you can also install third-party libraries to extend Python's functionality.
- NumPy: For numerical computations.
- Pandas: For data manipulation and analysis.
- Matplotlib: For creating static, animated, and interactive visualizations.
- Flask: A micro web framework for building web applications.
- Django: A high-level web framework for rapid web development.
- Scikit-learn: For machine learning algorithms and tools.
- TensorFlow: For deep learning and neural network models.
- Keras: A high-level neural networks API.
You can install external libraries using pip
, Python's package manager.
pip install numpy
import numpy as np
arr = np.array([1, 2, 3])
print(arr) # Output: [1 2 3]
Efficiency: Libraries help save time by offering pre-built solutions for common problems.
Reusability: Libraries allow you to reuse code and extend functionality without starting from scratch.
Community Support: Popular libraries often have large communities and documentation, making them easier to use.
- Git Installed
Make sure Git is installed on your system. You can verify this by running:git --version
PyCharm Installed Ensure you're using a recent version of PyCharm (Community or Professional edition).
🔧 Configuring Git in PyCharm Open PyCharm.
Go to File > Settings (or PyCharm > Preferences on macOS).
Navigate to Version Control > Git.
In the Path to Git executable, provide the correct Git path:
Windows: C:\Program Files\Git\bin\git.exe
macOS/Linux: Just use git (if it's in the system PATH)
Click the Test button to check if Git is detected.
Click OK to save the settings.
✅ Verifying Git Version in PyCharm Go to VCS > Git > Show Git Log to view commit history.
Or open the Terminal tab in PyCharm and run:
git --version
This guide demonstrates how to connect Python with a MySQL database using the mysql-connector-python
library.
- Python installed (3.x recommended)
- MySQL Server installed and running
- MySQL user credentials (username, password)
- Install MySQL connector:
pip install mysql-connector-python
import mysql.connector
# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="root",
password="your_password",
database="your_database"
)
# Create a cursor object
cursor = conn.cursor()
# Execute a query
cursor.execute("SELECT * FROM your_table")
# Fetch results
results = cursor.fetchall()
# Print results
for row in results:
print(row)
# Close the connection
cursor.close()
conn.close()
🛠️ Create Database and Table
python
Copy
Edit
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="root",
password="your_password"
)
cursor = conn.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS sample_db")
cursor.execute("USE sample_db")
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT
)
""")
conn.commit()
cursor.close()
conn.close()
This guide demonstrates how to perform Create, Read, Update, and Delete (CRUD) operations using Python and MongoDB via the pymongo
library.
- MongoDB installed and running locally or on the cloud (e.g., MongoDB Atlas)
- Python 3.x installed
- Install PyMongo:
pip install pymongo
# Importing pymongo
from pymongo import MongoClient
# Step 1: Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/") # Default host and port
db = client["mydatabase"] # Create or connect to a database
collection = db["students"] # Create or connect to a collection
# Step 2: Create (Insert documents)
student1 = {"name": "Guru", "age": 25, "course": "Python"}
student2 = {"name": "Mounesh", "age": 24, "course": "JavaScript"}
collection.insert_one(student1)
collection.insert_one(student2)
print("✅ Inserted documents.")
# Step 3: Read (Retrieve documents)
print("\n📖 All Students:")
for student in collection.find():
print(student)
# Step 4: Update (Modify documents)
query = {"name": "Guru"}
new_values = {"$set": {"age": 26}}
collection.update_one(query, new_values)
print("\n🔄 Updated Guru's age:")
print(collection.find_one({"name": "Guru"}))
# Step 5: Delete (Remove documents)
delete_query = {"name": "Mounesh"}
collection.delete_one(delete_query)
print("\n❌ Deleted Mounesh. Remaining documents:")
for student in collection.find():
print(student)
# Optional: Drop the collection (cleanup)
# collection.drop()
Pytest is a testing framework for Python that makes it easy to write simple as well as scalable test cases. It is used to ensure the correctness of your code and allows you to perform unit testing, integration testing, and functional testing.
- Simple syntax and powerful features
- Supports fixtures and parameterized testing
- Works with existing tests and test suites
- Provides detailed error messages
- Supports integration with continuous integration tools
You can install pytest using pip
:
pip install pytest
This file contains the functions to be tested.
# calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
In this step, we create a test file that uses pytest to test the functions in calculator.py.
# test_calculator.py
import pytest
from calculator import add, subtract
def test_add():
assert add(2, 3) == 5
def test_subtract():
assert subtract(5, 3) == 2
Running the Tests
To run the tests, use the following command in the terminal:
pytest test_calculator.py
When you run pytest on the test file, it will output the results:
$ pytest test_calculator.py
==================== test session starts ====================
collected 2 items
test_calculator.py .. [100%]
===================== 2 passed in 0.03 seconds =================