Python Notes
Python Notes
1. Introduction to Python:
Python is a popular, high-level programming
language known for its simplicity and readability.
It supports multiple programming paradigms such as procedural, object-oriented,
and functional programming.
Uses: Web development, data analysis, machine learning, automation, and more.
2. Python Features:
Simple: Python has a clear syntax that is
easy to understand.
Open-source: Python is free to use and distribute.
Interpreted: Python code is executed line-by-line, no need to compile.
Portable: Python works on different platforms (Windows, Mac, Linux).
Extensible: You can add low-level code to Python for performance.
Libraries: Python has a vast collection of libraries for different tasks (e.g.,
NumPy for math, Pandas for data handling, TensorFlow for AI).
3. Basic Syntax:
Variables: Used to store data. You don’t
need to declare the type of variable explicitly.
Example:
x = 5 # Integer
y = "Hello" # String
Comments: Use `#` to write a comment. Comments are ignored by the interpreter.
Example:
# This is a comment
print("Python is fun!") # This
prints a message
4. Data Types:
Numbers: Integers, floats (decimal
numbers), complex numbers.
Example:
a = 10 # Integer
b = 20.5 # Float
Strings: Text enclosed in quotes (`' '` or `" "`).
Example:
name = "Alice"
print(name)
Lists: A collection of items enclosed in square brackets.
Example:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
Dictionaries: A collection of key-value pairs.
Example:
person = {"name": "John", "age": 25}
print(person["name"]) #
Output: John
5. Control Structures:
If-Else Statements: Used to execute
different code based on conditions.
Example:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Loops:
- For Loop: Used to repeat a block of code.
Example:
for i in range(5):
print(i) # Prints 0 to 4
- While Loop: Repeats code while a condition is true.
Example:
x = 0
while x < 5:
print(x)
x += 1
6. Functions:
A function is a block of reusable code.
Example:
def greet(name):
return "Hello, " + name
print(greet("Alice")) #
Output: Hello, Alice
7. Classes and Objects:
Object-Oriented Programming (OOP): Python
supports OOP, where you can create classes and objects.
Class: A blueprint for objects.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return "Hello, my name is
" + self.name
p1 = Person("John", 30)
print(p1.greet()) # Output: Hello, my
name is John
8. Exception Handling:
Used to handle errors during program
execution.
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by
zero.")
finally:
print("This will always
run.")
9. Modules and Libraries:
Module: A file containing Python code that
can be reused. You can import and use it in your programs.
Example:
import math
print(math.sqrt(16)) # Output: 4.0
Popular libraries:
- NumPy: For numerical computations.
- Pandas: For data analysis.
- Matplotlib: For data visualization.
10. File Handling:
Python can read and write to files.
Example:
# Writing to a file
with open("file.txt", "w") as f:
f.write("Hello, world!")
# Reading from a file
with open("file.txt", "r") as f:
content = f.read()
print(content) # Output: Hello, world!
11. Conclusion:
Python is versatile and easy to learn.
It's a great language for beginners and is widely used in various industries,
from web development to artificial intelligence.
Comments
Post a Comment