PYTHON notes for beginner
PYTHON notes for beginner
1. Introduction to Python
Python is a high-level, interpreted programming language known for its simplicity and
readability. Created by Guido van Rossum and first released in 1991, Python has grown to
be one of the most popular programming languages due to its versatility and ease of use.
• Simple and Readable Syntax: Python's syntax is clear and concise, making it easy
to learn and write.
• Interpreted Language: Python code is executed line by line, which simplifies
debugging and enhances portability.
• Dynamic Typing: Variables in Python are dynamically typed, meaning you don’t
need to declare their data type explicitly.
• Extensive Standard Library: Python comes with a rich standard library that
supports many common programming tasks.
• Open Source: Python is freely available and its source code can be modified and
distributed.
• Support for Multiple Paradigms: Python supports procedural, object-oriented, and
functional programming styles.
4. Basic Syntax
• Print Statement:
python
print("Hello, World!")
• Comments:
python
# Single-line comment
"""
Multi-line comment
"""
python
x = 10
name = "Alice"
• Operators:
o Arithmetic Operators: +, -, *, /, //, %, **
o Comparison Operators: ==, !=, >, <, >=, <=
o Logical Operators: and, or, not
6. Control Structures
• If Statements:
python
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
python
for i in range(5):
print(i)
count = 0
while count < 5:
print(count)
count += 1
7. Functions
• Defining Functions:
python
def greet(name):
return "Hello, " + name
print(greet("Alice"))
8. Data Structures
python
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
python
coordinates = (10, 20)
print(coordinates[0]) # Output: 10
python
person = {"name": "John", "age": 30}
print(person["name"]) # Output: John
• Importing Modules:
python
import math
print(math.sqrt(16)) # Output: 4.0
• Try-Except Block:
python
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
• Reading a File:
python
with open("file.txt", "r") as file:
content = file.read()
print(content)
• Writing to a File:
python
with open("file.txt", "w") as file:
file.write("Hello, World!")
python
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return "Woof!"
my_dog = Dog("Buddy", 3)
print(my_dog.bark())
• Popular Libraries:
o NumPy: For numerical computations.
o Pandas: For data manipulation and analysis.
o Matplotlib: For plotting and visualization.
o Requests: For making HTTP requests.
• Web Frameworks:
o Django: A high-level web framework for rapid development.
o Flask: A lightweight web framework.
Feel free to ask if you have any specific questions or need more details on any topic!