PYTHON
About Lesson

What You Will Learn

In this course, you will learn the basics of Python programming, including:

  • Setting up Python
  • Understanding basic syntax and data types
  • Writing and running your first Python script
  • Using variables and operators
  • Working with strings, lists, and dictionaries
  • Writing functions
  • Introduction to object-oriented programming
  • Handling exceptions
  • Working with modules and packages

Setting Up Python

Before you start coding, you need to set up your Python environment. Here’s how to do it:

  1. Download Python: Visit the official Python website and download the latest version of Python for your operating system.
  2. Install Python: Run the installer and follow the instructions. Make sure to check the option to add Python to your PATH during the installation.
  3. Verify Installation: Open your terminal or command prompt and type python --version or python3 --version to verify that Python is installed correctly.

Writing Your First Python Script

Let’s write a simple Python script to get you started.

  1. Open a Text Editor: You can use any text editor you like, such as Notepad, Sublime Text, or Visual Studio Code.

  2. Write the Code: Type the following code into your text editor:

    python

    print("Hello, world!")
  3. Save the File: Save the file with a .py extension, for example, hello.py.

  4. Run the Script: Open your terminal or command prompt, navigate to the directory where you saved the file, and type python hello.py or python3 hello.py. You should see the output Hello, world!.

Basic Syntax and Data Types

Python’s syntax is clean and easy to understand. Here are some fundamental concepts:

  • Comments: Use # to add comments to your code.
  • Variables: Variables are used to store data. You don’t need to declare the type of a variable explicitly. Example: x = 5
  • Data Types: Common data types in Python include integers (int), floating-point numbers (float), strings (str), lists (list), and dictionaries (dict).

Example

Here’s a quick example to illustrate these concepts:

python

# This is a comment
x = 5 # Integer
y = 3.14 # Float
name = "Alice" # String
numbers = [1, 2, 3, 4, 5] # List
person = {"name": "Alice", "age": 25} # Dictionary

print(x)
print(y)
print(name)
print(numbers)
print(person)

Conclusion

Congratulations! You have written and run your first Python script and learned some basic concepts. In the next lesson, we will dive deeper into variables, operators, and data types. Keep practicing and experimenting with the examples provided.

Stay tuned for the next lesson!