0% found this document useful (0 votes)
6 views4 pages

1

Python is a high-level, interpreted programming language known for its readability and ease of use, making it popular among beginners and professionals alike. It supports dynamic typing, various data types, and object-oriented programming, while also providing features like functions, error handling, and file I/O. Python's extensive libraries and community support contribute to its versatility across different domains such as web development, data science, and machine learning.

Uploaded by

Minh Quang Ha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

1

Python is a high-level, interpreted programming language known for its readability and ease of use, making it popular among beginners and professionals alike. It supports dynamic typing, various data types, and object-oriented programming, while also providing features like functions, error handling, and file I/O. Python's extensive libraries and community support contribute to its versatility across different domains such as web development, data science, and machine learning.

Uploaded by

Minh Quang Ha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. What is Python, really?

Python is a high-level, interpreted programming language designed to be easy to


read and write. When you run a Python program, the interpreter (not a compiler)
reads your code line by line and executes it immediately. This makes Python
excellent for rapid prototyping, experimentation, and learning, though it can be
slower than compiled languages like C++ or Rust.
Python emphasizes clarity over cleverness. The syntax is clean and often resembles
pseudocode � code you might write on a whiteboard to explain logic. This is why
Python is a first choice for beginners, but it�s also powerful enough to be used by
Google, NASA, Netflix, and most machine learning research.

2. Variables and Data Types


Variables
In Python, you don�t declare types explicitly � you just assign values:
x = 42 # integer
name = "Alice" # string
pi = 3.14159 # floating-point
is_happy = True # boolean
Python is dynamically typed. The type of x can change later if you assign something
else:
x = "Hello!" # now it's a string
This is convenient but can cause bugs if you�re careless � many developers use type
hints (optional annotations) to add clarity:
def greet(name: str) -> str:
return f"Hello, {name}!"
Core Data Types
?
int � integers (5, -42)
?
?
float � real numbers (3.14, -0.001)
?
?
str � text ("Python")
?
?
bool � logical (True or False)
?
?
list � ordered collection ([1, 2, 3])
?
?
tuple � immutable ordered collection ((1, 2, 3))
?
?
dict � key-value map ({"name": "Alice", "age": 30})
?
?
set � unordered unique values ({1, 2, 3})
?

3. Control Flow: Decisions and Loops


Python code is structured using indentation, not curly braces. Indentation is not
just style � it�s part of the language.
If statements
age = 20if age >= 18:
print("Adult")elif age >= 13:
print("Teenager")else:
print("Child")
Loops
?
for loop (iterates over sequences):
?
for i in range(5): # 0,1,2,3,4
print(i)
?
while loop (runs until a condition is false):
?
count = 0while count < 3:
print("Hello")
count += 1

4. Functions
Functions group code into reusable blocks:
def add(a, b):
return a + b
print(add(3, 5)) # outputs 8
Functions can have default arguments:
def greet(name="world"):
print(f"Hello, {name}!")

greet() # Hello, world!


greet("Bob") # Hello, Bob!
They can also return multiple values (technically a tuple):
def divide(a, b):
quotient = a // b
remainder = a % b
return quotient, remainder

q, r = divide(10, 3) # q=3, r=1

5. Collections in Depth
Lists
Lists are mutable, ordered sequences:
nums = [1, 2, 3]
nums.append(4)
nums[0] = 10print(nums) # [10, 2, 3, 4]
Tuples
Tuples are immutable, often used for fixed data structures:
point = (3, 4)
x, y = point # tuple unpacking
Dictionaries
Dictionaries store data as key-value pairs:
person = {"name": "Alice", "age": 30}print(person["name"])
person["age"] = 31
Sets
Sets store unique items only:
unique = {1, 2, 2, 3}print(unique) # {1, 2, 3}

6. Strings
Strings are sequences of characters. You can use single ' or double " quotes, and
triple quotes for multiline:
text = "Hello"print(text.upper()) # HELLOprint(text[1]) #
eprint(text[::-1]) # reverse
Formatted strings (f-strings) make interpolation easy:
name = "Bob"
age = 25print(f"{name} is {age} years old")
7. Modules and Imports
A module is just a Python file with functions, classes, or variables you can reuse.
Use import:
import mathprint(math.sqrt(16)) # 4.0
You can also import selectively:
from math import pi, sin
Or rename imports:
import numpy as np

8. Error Handling
Python uses exceptions to signal errors:
try:
result = 10 / 0except ZeroDivisionError:
print("Cannot divide by zero")finally:
print("This always runs")
This prevents your program from crashing abruptly.

9. Classes and Objects


Python supports object-oriented programming (OOP). A class is a blueprint for
creating objects (instances):
class Dog:
def __init__(self, name):
self.name = name

def bark(self):
print(f"{self.name} says woof!")

dog = Dog("Fido")
dog.bark()
?
__init__ is the constructor.
?
?
self refers to the current object.
?
?
Attributes (self.name) hold data.
?
?
Methods (bark) define behavior.
?

10. Files and I/O


Reading and writing files is straightforward:
# Writewith open("hello.txt", "w") as f:
f.write("Hello, world!")
# Readwith open("hello.txt", "r") as f:
content = f.read()
print(content)
The with statement automatically closes the file, even if errors occur.

11. Pythonic Style


�Pythonic� means writing code that feels natural in Python:
?
Use list comprehensions:
?
squares = [x*x for x in range(5)]
?
Use enumerate for loops:
?
for i, val in enumerate(["a","b"]):
print(i, val)
?
Avoid reinventing the wheel � Python�s standard library is rich and powerful.
?

12. Virtual Environments and Packages


Python has thousands of external libraries. To avoid conflicts, developers use
virtual environments:
python -m venv venvsource venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
pip install requests
This keeps project dependencies isolated.

13. Advanced Features You�ll See Around


?
Generators (yield) for streaming data efficiently.
?
?
Decorators (@something) to modify functions dynamically.
?
?
Type hints with typing module to write self-documenting code.
?
?
Asynchronous programming (async/await) for concurrent tasks.
?

14. Why Python is So Popular


?
Readability: Syntax is clear and friendly.
?
?
Huge ecosystem: Libraries for data science, AI, web, automation.
?
?
Community support: Tons of tutorials, Q&A, documentation.
?
?
Versatility: Works for scripts, websites, games, machine learning, hardware control
� you name it.
?

You might also like