-
@ Marth
2024-09-19 20:54:23Python Introduction Guide
1. What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability. It's widely used in various fields, including web development, data science, artificial intelligence, and more.
2. Installing Python
Visit python.org to download and install the latest version of Python for your operating system.
3. Your First Python Program
Open a text editor and type:
python print("Hello, World!")
Save this as
hello.py
and run it from the command line:python hello.py
4. Basic Syntax
Variables and Data Types
```python
Strings
name = "Alice"
Integers
age = 30
Floats
height = 5.5
Booleans
is_student = True ```
Lists
python fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Outputs: apple
Dictionaries
python person = { "name": "Bob", "age": 25, "city": "New York" } print(person["name"]) # Outputs: Bob
5. Control Flow
If statements
python age = 18 if age >= 18: print("You're an adult") elif age >= 13: print("You're a teenager") else: print("You're a child")
Loops
```python
For loop
for i in range(5): print(i)
While loop
count = 0 while count < 5: print(count) count += 1 ```
6. Functions
```python def greet(name): return f"Hello, {name}!"
message = greet("Alice") print(message) # Outputs: Hello, Alice! ```
7. Modules
Python has a vast standard library and many third-party packages. You can import them like this:
python import math print(math.pi) # Outputs: 3.141592653589793
8. Exception Handling
python try: result = 10 / 0 except ZeroDivisionError: print("Error: Division by zero!")
9. Classes and Objects
```python class Dog: def init(self, name): self.name = name
def bark(self): return f"{self.name} says Woof!"
my_dog = Dog("Buddy") print(my_dog.bark()) # Outputs: Buddy says Woof! ```
10. Next Steps
- Practice writing small programs
- Explore the Python standard library
- Learn about virtual environments and package management with pip
- Dive into specific areas like web development (Django, Flask) or data science (NumPy, Pandas)
Remember, the best way to learn Python is by writing code. Happy coding!