Python Basics Guide
1. Introduction to Python
Python is an interpreted, high-level programming language known for its simplicity and readability. It
is widely used in web development, data science, automation, and finance.
Python Features:
- Easy to learn
- Large community support
- Versatile
- Extensive libraries
Installing Python:
- Download from https://www.python.org
- Install Anaconda for data science work.
Running Python Code:
- Use the Python shell
- Write scripts (.py files)
- Use Jupyter Notebook for interactive coding
2. Python Syntax and Basic Operations
Python uses indentation instead of braces. Example:
```python
print('Hello, World!')
```
Basic Operations:
```python
x = 10
y=5
sum = x + y
Python Basics Guide
print(sum)
```
Python follows dynamic typing, meaning variable types do not need to be declared.
3. Data Types and Type Conversion
Common Data Types:
- Integers (int)
- Floating-point numbers (float)
- Strings (str)
- Boolean (bool)
Example:
```python
x=5
y = 3.14
text = 'Hello'
flag = True
```
Type Conversion:
```python
num = '10'
num = int(num) # Convert string to integer
```
4. Strings and String Manipulation
Working with Strings:
```python
text = 'Python is fun'
Python Basics Guide
print(text.upper())
print(text.lower())
print(text.replace('fun', 'awesome'))
```
5. Lists, Tuples, and Dictionaries
Lists store multiple values:
```python
fruits = ['Apple', 'Banana', 'Cherry']
print(fruits[0])
```
Dictionaries store key-value pairs:
```python
person = {'name': 'Alice', 'age': 25}
print(person['name'])
```
6. Conditional Statements
If-Else Example:
```python
num = 10
if num > 5:
print('Big number')
else:
print('Small number')
```
7. Loops in Python
Python Basics Guide
For Loop:
```python
for i in range(5):
print(i)
```
While Loop:
```python
x=0
while x < 3:
print(x)
x += 1
```
8. Functions and Modules
Defining Functions:
```python
def greet(name):
return 'Hello, ' + name
print(greet('Alice'))
```
9. File Handling
Reading and writing files:
```python
with open('data.txt', 'w') as file:
file.write('Hello, Python!')
```
Reading from a file:
Python Basics Guide
```python
with open('data.txt', 'r') as file:
content = file.read()
print(content)
```
10. Error Handling
Using Try-Except:
```python
try:
x=1/0
except ZeroDivisionError:
print('Cannot divide by zero!')
```
11. Working with NumPy and Pandas
NumPy arrays:
```python
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
```
12. Data Analysis with Pandas
Creating DataFrame:
```python
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
Python Basics Guide
print(df)
```
13. Data Visualization with Matplotlib
Plotting data:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
plt.show()
```
14. Fetching Financial Data with yfinance
Fetching stock data:
```python
import yfinance as yf
stock = yf.Ticker('AAPL')
df = stock.history(period='1mo')
print(df.head())
```
15. Python Automation with Selenium
Web automation:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://google.com')
```
Python Basics Guide
16. Machine Learning Basics
Using scikit-learn:
```python
from sklearn.linear_model import LinearRegression
model = LinearRegression()
```
17. Python Project Ideas
1. To-do list app
2. Web scraper
3. Stock price predictor
4. Data visualization dashboard
5. Automation script
18. Debugging and Optimization
Debugging tips:
- Use print statements
- Use a debugger
- Write clean, modular code
19. Hands-on Exercises
1. Write a Python program to reverse a string.
2. Fetch stock data for Tesla and plot its prices.
3. Build a simple calculator.
4. Automate form-filling with Selenium.
5. Analyze CSV data using Pandas.
20. Next Steps
Python Basics Guide
Further Learning:
- Practice Python on W3Schools (https://www.w3schools.com/python/)
- Take online courses on Udemy, Coursera, or Codecademy
- Contribute to open-source projects on GitHub
- Build small projects to apply your knowledge
1. Introduction to Python
Python is an interpreted, high-level programming language known for its simplicity and readability. It
is widely used in web development, data science, automation, and finance.
Python Features:
- Easy to learn
- Large community support
- Versatile
- Extensive libraries
Installing Python:
- Download from https://www.python.org
- Install Anaconda for data science work.
Running Python Code:
- Use the Python shell
- Write scripts (.py files)
- Use Jupyter Notebook for interactive coding
2. Python Syntax and Basic Operations
Python uses indentation instead of braces. Example:
```python
print('Hello, World!')
```
Python Basics Guide
Basic Operations:
```python
x = 10
y=5
sum = x + y
print(sum)
```
Python follows dynamic typing, meaning variable types do not need to be declared.
3. Data Types and Type Conversion
Common Data Types:
- Integers (int)
- Floating-point numbers (float)
- Strings (str)
- Boolean (bool)
Example:
```python
x=5
y = 3.14
text = 'Hello'
flag = True
```
Type Conversion:
```python
num = '10'
num = int(num) # Convert string to integer
Python Basics Guide
```
4. Strings and String Manipulation
Working with Strings:
```python
text = 'Python is fun'
print(text.upper())
print(text.lower())
print(text.replace('fun', 'awesome'))
```
5. Lists, Tuples, and Dictionaries
Lists store multiple values:
```python
fruits = ['Apple', 'Banana', 'Cherry']
print(fruits[0])
```
Dictionaries store key-value pairs:
```python
person = {'name': 'Alice', 'age': 25}
print(person['name'])
```
6. Conditional Statements
If-Else Example:
```python
num = 10
if num > 5:
Python Basics Guide
print('Big number')
else:
print('Small number')
```
7. Loops in Python
For Loop:
```python
for i in range(5):
print(i)
```
While Loop:
```python
x=0
while x < 3:
print(x)
x += 1
```
8. Functions and Modules
Defining Functions:
```python
def greet(name):
return 'Hello, ' + name
print(greet('Alice'))
```
9. File Handling
Python Basics Guide
Reading and writing files:
```python
with open('data.txt', 'w') as file:
file.write('Hello, Python!')
```
Reading from a file:
```python
with open('data.txt', 'r') as file:
content = file.read()
print(content)
```
10. Error Handling
Using Try-Except:
```python
try:
x=1/0
except ZeroDivisionError:
print('Cannot divide by zero!')
```
11. Working with NumPy and Pandas
NumPy arrays:
```python
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
```
Python Basics Guide
12. Data Analysis with Pandas
Creating DataFrame:
```python
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
```
13. Data Visualization with Matplotlib
Plotting data:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
plt.show()
```
14. Fetching Financial Data with yfinance
Fetching stock data:
```python
import yfinance as yf
stock = yf.Ticker('AAPL')
df = stock.history(period='1mo')
print(df.head())
```
15. Python Automation with Selenium
Python Basics Guide
Web automation:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://google.com')
```
16. Machine Learning Basics
Using scikit-learn:
```python
from sklearn.linear_model import LinearRegression
model = LinearRegression()
```
17. Python Project Ideas
1. To-do list app
2. Web scraper
3. Stock price predictor
4. Data visualization dashboard
5. Automation script
18. Debugging and Optimization
Debugging tips:
- Use print statements
- Use a debugger
- Write clean, modular code
19. Hands-on Exercises
1. Write a Python program to reverse a string.
Python Basics Guide
2. Fetch stock data for Tesla and plot its prices.
3. Build a simple calculator.
4. Automate form-filling with Selenium.
5. Analyze CSV data using Pandas.
20. Next Steps
Further Learning:
- Practice Python on W3Schools (https://www.w3schools.com/python/)
- Take online courses on Udemy, Coursera, or Codecademy
- Contribute to open-source projects on GitHub
- Build small projects to apply your knowledge
1. Introduction to Python
Python is an interpreted, high-level programming language known for its simplicity and readability. It
is widely used in web development, data science, automation, and finance.
Python Features:
- Easy to learn
- Large community support
- Versatile
- Extensive libraries
Installing Python:
- Download from https://www.python.org
- Install Anaconda for data science work.
Running Python Code:
- Use the Python shell
- Write scripts (.py files)
- Use Jupyter Notebook for interactive coding
Python Basics Guide
2. Python Syntax and Basic Operations
Python uses indentation instead of braces. Example:
```python
print('Hello, World!')
```
Basic Operations:
```python
x = 10
y=5
sum = x + y
print(sum)
```
Python follows dynamic typing, meaning variable types do not need to be declared.
3. Data Types and Type Conversion
Common Data Types:
- Integers (int)
- Floating-point numbers (float)
- Strings (str)
- Boolean (bool)
Example:
```python
x=5
y = 3.14
text = 'Hello'
flag = True
```
Python Basics Guide
Type Conversion:
```python
num = '10'
num = int(num) # Convert string to integer
```
4. Strings and String Manipulation
Working with Strings:
```python
text = 'Python is fun'
print(text.upper())
print(text.lower())
print(text.replace('fun', 'awesome'))
```
5. Lists, Tuples, and Dictionaries
Lists store multiple values:
```python
fruits = ['Apple', 'Banana', 'Cherry']
print(fruits[0])
```
Dictionaries store key-value pairs:
```python
person = {'name': 'Alice', 'age': 25}
print(person['name'])
```
6. Conditional Statements
Python Basics Guide
If-Else Example:
```python
num = 10
if num > 5:
print('Big number')
else:
print('Small number')
```
7. Loops in Python
For Loop:
```python
for i in range(5):
print(i)
```
While Loop:
```python
x=0
while x < 3:
print(x)
x += 1
```
8. Functions and Modules
Defining Functions:
```python
def greet(name):
return 'Hello, ' + name
print(greet('Alice'))
Python Basics Guide
```
9. File Handling
Reading and writing files:
```python
with open('data.txt', 'w') as file:
file.write('Hello, Python!')
```
Reading from a file:
```python
with open('data.txt', 'r') as file:
content = file.read()
print(content)
```
10. Error Handling
Using Try-Except:
```python
try:
x=1/0
except ZeroDivisionError:
print('Cannot divide by zero!')
```
11. Working with NumPy and Pandas
NumPy arrays:
```python
import numpy as np
Python Basics Guide
arr = np.array([1, 2, 3])
print(arr)
```
12. Data Analysis with Pandas
Creating DataFrame:
```python
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
```
13. Data Visualization with Matplotlib
Plotting data:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
plt.show()
```
14. Fetching Financial Data with yfinance
Fetching stock data:
```python
import yfinance as yf
stock = yf.Ticker('AAPL')
df = stock.history(period='1mo')
Python Basics Guide
print(df.head())
```
15. Python Automation with Selenium
Web automation:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://google.com')
```
16. Machine Learning Basics
Using scikit-learn:
```python
from sklearn.linear_model import LinearRegression
model = LinearRegression()
```
17. Python Project Ideas
1. To-do list app
2. Web scraper
3. Stock price predictor
4. Data visualization dashboard
5. Automation script
18. Debugging and Optimization
Debugging tips:
- Use print statements
- Use a debugger
Python Basics Guide
- Write clean, modular code
19. Hands-on Exercises
1. Write a Python program to reverse a string.
2. Fetch stock data for Tesla and plot its prices.
3. Build a simple calculator.
4. Automate form-filling with Selenium.
5. Analyze CSV data using Pandas.
20. Next Steps
Further Learning:
- Practice Python on W3Schools (https://www.w3schools.com/python/)
- Take online courses on Udemy, Coursera, or Codecademy
- Contribute to open-source projects on GitHub
- Build small projects to apply your knowledge