Tutorial: Introduction to Python

Get started with Python, a versatile and beginner-friendly programming language.

By Upingi Team / Tutorial Level: Beginner

Why Learn Python?

Python is known for its readability and simple syntax, making it an excellent first language. It's widely used in web development, data science, automation, machine learning, and more.

Learning Python opens doors to numerous career paths and allows you to build a wide variety of applications.

Prerequisites

  • Install Python: Download and install Python from the official Python website. Make sure to add Python to your system's PATH during installation.
  • Text Editor or IDE: Use a simple text editor or an Integrated Development Environment (IDE) like VS Code, PyCharm, or Sublime Text.

Let's write some Python code!

Chapter 1: Your First Python Script

Let's start with the traditional "Hello, World!" example.

  1. Create a file: Make a new file named `hello.py`.
  2. Write the code: Add the following line: `print("Hello, World!")`.
  3. Run the script: Open your terminal, navigate to the directory containing `hello.py`, and run `python hello.py` (or `python3 hello.py` depending on your system).
  4. Variables: Assign values to names: `message = "Hello again"`, then `print(message)`.
  5. Data Types (Intro): Briefly mention strings (`"text"`), integers (`10`), and floats (`3.14`).

You've executed your first Python code!

Chapter 2: Lists and Loops

Lists are ordered, mutable sequences used to store collections of items. You can iterate over lists using loops.

Creating Lists: Use square brackets `[]`.

# Empty list
my_list = []

# List of numbers
numbers = [1, 5, 2, 8]

# List of strings
names = ["Alice", "Bob", "Charlie"]

# Mixed list
mixed = [10, "Hello", 3.14]

Accessing Elements: Use zero-based indexing.

print(names[0])  # Output: Alice
print(numbers[1])  # Output: 5

Looping with `for`: Iterate through each item in a list.

for name in names:
    print(f"Hello, {name}!")
# Output:
# Hello, Alice!
# Hello, Bob!
# Hello, Charlie!

Dictionaries (Brief Intro): Dictionaries store key-value pairs, enclosed in curly braces `{}`.

person = {"name": "Alice", "age": 30}
print(person["name"]) # Output: Alice

Conclusion & Next Steps

Congratulations on starting your Python journey! You've learned how to run scripts, use variables, and understand basic data types.

Keep exploring:

  • Learn about conditional statements (`if`, `elif`, `else`).
  • Explore functions to organize your code.
  • Dive deeper into data structures like lists, tuples, dictionaries, and sets.
  • Start working on small projects to practice your skills.
  • Discover Python libraries for specific tasks (e.g., `requests` for web, `pandas` for data).