Python Basics
Python is a powerful, high-level, and easy-to-learn programming
language. It is widely used for web development, data science,
automation, AI, and more.
## Variables and Data Types:
Python supports multiple data types such as:
- Integer (`int`): Whole numbers like 1, 100, -5
- Floating Point (`float`): Numbers with decimals like 2.5, -0.99
- String (`str`): Text data like "Hello World"
- Boolean (`bool`): `True` or `False`
- Lists, Tuples, Dictionaries, and Sets
### Example:
```python
x = 10 # Integer
y = 3.14 # Float
name = "John" # String
is_valid = True # Boolean
```
## Conditional Statements:
Python supports `if`, `elif`, and `else` for decision-making.
```python
age = 20
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
```
## Loops:
Python has `for` and `while` loops.
```python
for i in range(5):
print(i)
count = 0
while count < 5:
print(count)
count += 1
```
NumPy
NumPy (Numerical Python) is used for scientific computing,
supporting arrays and mathematical functions.
## Installing NumPy:
```bash
pip install numpy
```
## Creating Arrays:
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
```
## Array Operations:
```python
a = np.array([10, 20, 30])
b = np.array([5, 10, 15])
print(a + b) # Addition
print(a * 2) # Scalar multiplication
```
## Reshaping Arrays:
```python
matrix = np.array([[1, 2], [3, 4]])
reshaped = matrix.reshape(1, 4)
print(reshaped)
```
Pandas
Pandas is used for data manipulation and analysis.
## Creating DataFrames:
```python
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
```
## Reading CSV Files:
```python
df = pd.read_csv('data.csv')
print(df.head())
```
## Handling Missing Values:
```python
df.fillna(0, inplace=True)
print(df)
```
OOP in Python
Object-Oriented Programming (OOP) allows modular and reusable
code.
## Creating a Class:
```python
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display(self):
print(f'Car: {self.brand} {self.model}')
car1 = Car('Tesla', 'Model S')
car1.display()
```
## Inheritance:
```python
class ElectricCar(Car):
def __init__(self, brand, model, battery):
super().__init__(brand, model)
self.battery = battery
def show_battery(self):
print(f'Battery: {self.battery} kWh')
car2 = ElectricCar('Tesla', 'Model 3', 75)
car2.display()
car2.show_battery()
```
TensorFlow
TensorFlow is an open-source deep learning framework.
## Installing TensorFlow:
```bash
pip install tensorflow
```
## Basic TensorFlow Example:
```python
import tensorflow as tf
x = tf.constant(5)
y = tf.constant(6)
z=x*y
print(f'Result: {z.numpy()}')
```
Git
Git is a version control system used to track code changes.
## Installing Git:
```bash
# Ubuntu/Debian
sudo apt install git
# Mac (Homebrew)
brew install git
```
## Git Commands:
```bash
git init # Initialize a repository
git status # Check status
git add . # Add all files
git commit -m "Message" # Commit changes
git push origin main # Push changes to GitHub
git pull origin main # Pull latest updates
```