Table of Contents
Introduction to Python
Welcome to Day 1 of our Python curriculum! In this lesson, we’ll introduce you to the Python programming language and get you started with writing your first program.
Installing Python
Before we can start programming in Python, we need to install it on our computer. You can download the latest version of Python from the official website (https://www.python.org/downloads/). Follow the installation instructions on the website to install Python on your computer.
Access the REPL
The quickest way to get the first taste of Python is to use Python REPL. You can access it by opening your terminal (or powershell) and type python3
Now you can type anything into this console:
Basic Syntax
Python is a high-level, interpreted programming language. This means that we can write Python code and run it without needing a separate compilation step. Here’s a basic “Hello, World!” program in Python:
print("Hello datmt.com")
In this example, we use the print()
function to output the string “Hello, World!” to the console. The parentheses after the function name are used to pass arguments to the function.
Python code is organized into logical blocks using indentation. Blocks of code that are indented at the same level belong to the same block. For example, here’s a program that uses an if
statement to print out a message if a condition is true:
x = 10 if x > 5: print("x is greater than 5")
Data Types and Variables
Python supports various data types, including integers, floating-point numbers, strings, and booleans. We can use variables to store the values of these data types. Here’s an example:
x = 10 y = 3.14 name = "Alice" is_student = True
More Exercises
Now that you’ve learned about Python’s basic syntax, data types, and variables, it’s time to practice! Here are a few exercises:
Write a program that calculates the area of a rectangle with a width of 5 units and a height of 10 units.
width = 5 height = 10 area = width * height # print the area print(area)
Write a program that prompts the user to enter their name and age, and then outputs a greeting message with their name and age.
This practice helps you learn how to get input from users.
name = input("What is your name? ") age = int(input("How old are you? ")) print("Hello, " + name + "! You are " + str(age) + " years old.")
For this program, you need to store it in a file. After that, you can run it by typing: python3 file.py.
Here is the output:
Conclusion
Congratulations on completing Day 1 of our Python curriculum! In this lesson, I introduced you to Python’s basic syntax, data types, and variables, and got you started with writing your first program. Stay tuned for Day 2, where we’ll be covering control structures in Python. Happy coding!
You can get the code for this post here on GitHub.
I build softwares that solve problems. I also love writing/documenting things I learn/want to learn.