Python Basics Guide
1. Introduction to Python
Python is a high-level programming language known for its readability and versatility. It is widely
used in data science, web development, automation, and finance.
Why use Python?
- Simple and easy to learn
- Huge community support
- Extensive libraries
Installing Python:
Download Python from https://www.python.org and install it.
2. Basic Syntax, Variables, and Data Types
Python uses indentation instead of braces.
Example:
```python
print('Hello, World!')
```
Variables store data:
```python
name = 'Alice'
age = 25
height = 5.8
print(name, age, height)
```
Data Types:
- int (integer)
- float (decimal)
Python Basics Guide
- str (string)
- bool (True/False)
3. Control Flow (If-Else, Loops)
Python supports conditions and loops:
If-Else:
```python
num = 10
if num > 5:
print('Big number')
else:
print('Small number')
```
For Loop:
```python
for i in range(5):
print(i)
```
While Loop:
```python
x=0
while x < 3:
print(x)
x += 1
```
4. Functions
Python Basics Guide
Functions help organize reusable code:
```python
def greet(name):
return 'Hello, ' + name
print(greet('Alice'))
```
5. Lists, Tuples, and Dictionaries
Lists store multiple values:
```python
fruits = ['Apple', 'Banana', 'Cherry']
print(fruits[0])
```
Tuples (Immutable Lists):
```python
dimensions = (1920, 1080)
print(dimensions[0])
```
Dictionaries store key-value pairs:
```python
person = {'name': 'Alice', 'age': 25}
print(person['name'])
```
6. File Handling
Reading and writing files:
```python
with open('data.txt', 'w') as file:
Python Basics Guide
file.write('Hello, Python!')
```
Reading from a file:
```python
with open('data.txt', 'r') as file:
content = file.read()
print(content)
```
7. Working with Libraries (NumPy, Pandas, Matplotlib)
Libraries make Python powerful.
Install required libraries:
```bash
pip install numpy pandas matplotlib
```
Using Pandas to create a DataFrame:
```python
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
```
8. Handling CSV and Financial Data with yfinance
Fetching stock data with yfinance:
```python
import yfinance as yf
Python Basics Guide
stock = yf.Ticker('AAPL')
df = stock.history(period='1mo')
print(df.head())
```
9. Practical Exercises
Try these exercises:
1. Write a function that checks if a number is even or odd.
2. Create a list of numbers from 1 to 10 and print only the even numbers.
3. Write a Python program to count the occurrences of each word in a text file.
4. Fetch stock data for 'TSLA' and plot its closing prices using Matplotlib.
5. Create a dictionary with employee names and salaries, then print salaries above 5000.
10. Next Steps
Where to go from here?
- Practice Python on W3Schools (https://www.w3schools.com/python/)
- Solve coding problems on LeetCode or HackerRank
- Build projects like a to-do list, a web scraper, or a stock analysis tool
- Learn data science libraries (Pandas, NumPy, Matplotlib) for financial analysis