Lesson 1: Introduction to Python Programming



Overview

In this lesson, we'll cover the basics of Python programming. We'll start with an overview of what Python is and why it's a popular programming language. Then, we'll install Python and a text editor, and learn how to run our first "Hello, World!" program.

Learning Objectives

By the end of this lesson, you should be able to:

- Explain what Python is and why it's used.

- Install Python and a text editor on your computer.

- Write and run a simple Python program.


Topics Covered

- What is Python?

Python is a high-level, interpreted programming language that is known for its simplicity, readability, and ease of use. It was first created in the late 1980s by Guido van Rossum and has since become one of the most popular programming languages in the world. Python can be used for a wide variety of tasks such as web development, scientific computing, data analysis, artificial intelligence, machine learning, and more. The language is open-source, meaning that its source code is freely available to developers who want to modify or contribute to it.

- Installing Python and a text editor

To install Python and a text editor, you can follow these general steps:

1. Download and install Python: You can download the latest version of Python from the official website at https://www.python.org/downloads/. Follow the installation instructions provided for your specific operating system.

2. Choose a text editor: There are many text editors available that you can use to write Python code. Some popular options include Visual Studio Code, Sublime Text, Atom, PyCharm, and Spyder. Choose one that meets your needs and preferences.

3. Install and configure the text editor: Once you have chosen a text editor, download and install it on your computer. Then, configure it for Python development by installing any necessary plugins or extensions. 

4. Start coding: With Python and a text editor installed, you can start writing your Python code. Open your text editor and create a new file

- Running a Python program

To run a Python program, you can follow these general steps:

1. Open your text editor: First, open the text editor that you used to write your Python code.

2. Open the Python file: Navigate to the location where your Python file is saved and open it in your text editor.

3. Check your code: Before running your program, double-check your code to make sure that there are no syntax errors or other issues. Fix any errors as necessary.

4. Run the program: Once your code is error-free, you can run your program by executing the Python interpreter on the command line. To do this, open a terminal or command prompt and navigate to the directory where your Python file is located. Then, enter the following command:

Copy Code
python filename.py

Replace `filename` with the name of your Python file (including the .py extension).

5. View the output: After running your program, check the output in the terminal or command prompt window. If your program produces any output, it should be displayed here.

Note that the exact steps for running a Python program may vary depending on your operating system and the specific text editor you choose to use.


- Printing output with `print()`

`print()` is a built-in Python function that allows you to display output on the console. You can pass one or more values to `print()`, which will be converted to strings and displayed on the console.

Here's an example of how to use `print()` to display a string:

Copy Code
print("Hello, World!")

This will print the string "Hello, World!" to the console.

You can also pass multiple arguments to `print()` by separating them with commas. Here's an example:

Copy Code
name = "John" age = 30 print("My name is", name, "and I am", age, "years old.")

This will print the string "My name is John and I am 30 years old." to the console.

Additionally, you can use formatting to control how your output is displayed using special syntax. Here's an example:

Copy Code
pi = 3.14159 print("The value of pi is {:.2f}".format(pi))

This will print the string "The value of pi is 3.14" to the console, because we used formatting to round the value of `pi` to two decimal places.

 

- Working with variables in Python

In Python, variables are used to store data values. You can think of a variable as a container that holds a value. To create a variable in Python, you simply need to give it a name and assign it a value using the equal sign (=) operator.


Here's an example:

Copy Code
# Assigning a value to a variable x = 5 # Printing the value of the variable print(x)

In this example, we're creating a variable named `x` and assigning it the value of 5. We're then printing the value of `x`.

You can also assign different types of values to a variable in Python. For example, you can assign a string, a list, or even a function to a variable.

Here are some more examples:

Copy Code
# Assigning a string to a variable message = "Hello, World!" # Printing the value of the variable print(message) # Assigning a list to a variable my_list = [1, 2, 3, 4, 5] # Printing the value of the variable print(my_list) # Assigning a function to a variable def add_numbers(x, y): return x + y my_function = add_numbers # Using the variable to call the function result = my_function(3, 5) print(result)


- Basic data types (strings, numbers, booleans)


In Python, there are several basic data types:

1. Strings - They are used to represent text and are enclosed in either single or double quotes ('hello' or "world").

2. Numbers - They can be of two types: integers (whole numbers) or floating-point numbers (numbers with decimal points).

3. Booleans - They represent the truth values True and False.

 

Here's an example of each data type in Python:

pythonCopy Code
# String greeting = "Hello, world!" print(greeting) # Integer age = 25 print(age) # Floating-point number pi = 3.14159 print(pi) # Boolean is_python_fun = True print(is_python_fun)


Output:


Copy Code
Hello, world! 25 3.14159 True


Activities

- Watch a video tutorial on installing Python and a text editor (e.g. VS Code or Sublime Text).

- Follow along with the instructor as they write and run a "Hello, World!" program.

- Complete a short quiz to check your understanding of basic Python concepts.

- Write your own simple program using variables and `print()` statements.


Resources

- Python documentation: https://www.python.org/doc/

- Python beginner's guide: https://wiki.python.org/moin/BeginnersGuide


Next Steps

Once you've completed this lesson, you should have a basic understanding of Python syntax and how to write and run a simple program. In the next lesson, we'll dive deeper into data types and operators. 

Next Post
No Comment
Add Comment
comment url