Python is a high-level, interpreted programming language that is widely used for various applications including web development, scientific computing, data analysis, and more. Its simplicity and readability make it an ideal language for beginners who want to get started with programming.
In this tutorial, we will cover the fundamentals of Python programming and guide you through writing your first Python program. So, let’s dive in and get started!
Before we begin writing code, make sure you have Python installed on your computer. You can download the latest version of Python from the official website at python.org. Choose the appropriate installer for your operating system and follow the installation instructions.
Once Python is installed, you can verify the installation by opening a terminal or command prompt and typing the following command:
python --version
You should see the version number of Python displayed, indicating that it is installed correctly.
Now that we have Python set up, let’s write a simple “Hello, World!” program to get started. Open a text editor or an Integrated Development Environment (IDE) such as PyCharm or Visual Studio Code, and create a new file with a .py extension, for example, hello_world.py.
In this file, type the following code:
print("Hello, World!")
Save the file and navigate to the directory where you saved it using the terminal or command prompt. To run the program, type the following command:
python hello_world.py
You should see the output Hello, World! printed to the console.
Congratulations! You have written and executed your first Python program. Let’s break down the code to understand what each line does:
print("Hello, World!"): The print() function is used to display text on the console. In this case, it prints the string "Hello, World!".In Python, variables are used to store values that can be used later in the program. Unlike other programming languages, Python is dynamically typed, which means you don’t have to explicitly declare the type of a variable before assigning a value to it.
Let’s create a simple program that calculates the area of a rectangle. Open a new file and enter the following code:
length = 5
width = 3
area = length * width
print("The area of the rectangle is:", area)
Save the file and run the program. You should see the output The area of the rectangle is: 15 displayed on the console.
In this example, we declare two variables length and width and assign them the values 5 and 3 respectively. We then calculate the area by multiplying the length and width and store the result in the variable area. Finally, we use the print() function to display the calculated area on the console.
Python supports different data types such as integer, float, string, boolean, and more. In the example above, we used integer values, but you can assign values of different types to variables based on your requirements.
Python provides various control structures to execute specific code blocks based on certain conditions. Let’s explore the if...else statement to make decisions in our program.
number = 4
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Save the file and run the program. If the value of number is even, it will print "The number is even."; otherwise, it will print "The number is odd.".
In this example, we use the modulo operator % to check if number is divisible evenly by 2. If the remainder is 0, the number is even; otherwise, it is odd. Based on the result of the condition, the program executes the corresponding code block.
In this tutorial, we covered the basics of Python programming and wrote our first Python program. We learned about setting up Python, printing text to the console, using variables and data types, as well as making decisions using control flow statements.
Python is a versatile language that offers a wide range of features and libraries for various application domains. This tutorial only scratches the surface, but it should provide you with a solid foundation to continue your learning journey.
Happy coding!