DEV Community

Cover image for Python Tutorial: Code Your First Program
Python Tutorial
Python Tutorial

Posted on

Python Tutorial: Code Your First Program

Python is one of the most beginner-friendly programming languages, making it an excellent choice for those looking to dive into coding. This Python tutorial will guide you step by step to write your first Python program. Whether you are new to coding or want to refresh your skills, this tutorial will help you learn Python programming with ease.


Why Learn Python?

Python is widely used in web development, data science, artificial intelligence, automation, and more. Its simple syntax and readability make it an ideal choice for beginners. With Python, you can build applications quickly and efficiently without the complexity of other programming languages.


Setting Up Python

Before writing your first program, you need to install Python on your computer. You can learn python from our official website Tpoint Tech and follow the written instructions. Once learned, you can run Python code using Online Python Compiler for more knowledge.


Writing Your First Python Program

Now, let's get started with writing your first Python program. Open your preferred Python environment and type the following:

print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

This simple command tells Python to display "Hello, World!" on the screen. To run the program, save the file with a .py extension and execute it. You should see the output displayed in the terminal or console.


Understanding the Code

In this example, we use the print() function, which is one of the most fundamental functions in Python. It allows us to output text or data to the screen. The text inside the parentheses is enclosed in double quotes ("") or single quotes (''), which are used to define strings in Python.


Taking User Input

Now that you have successfully printed text, let's make the program interactive by allowing user input. Try the following code:

name = input("Enter your name: ")
print("Hello, " + name + "!")
Enter fullscreen mode Exit fullscreen mode

Here’s how the code works:

  1. The input() function asks the user to enter their name.
  2. The input is stored in a variable called name.
  3. The print() function then displays a greeting using the entered name. ### Variables and Data Types Python supports different data types such as:
  4. Strings (str) – text values ("Hello", "Python")
  5. Integers (int) – whole numbers (10, 50, 100)
  6. Floats (float) – decimal numbers (3.14, 9.8)
  7. Booleans (bool)True or False values

Example of variable assignment:

age = 25  # Integer
height = 5.9  # Float
is_student = True  # Boolean
print("Age:", age, "Height:", height, "Student:", is_student)
Enter fullscreen mode Exit fullscreen mode

Writing a Simple Calculator

Let’s create a simple calculator that adds two numbers:

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum = num1 + num2
print("The sum is:", sum)
Enter fullscreen mode Exit fullscreen mode

This program:

  1. Takes two numbers as input from the user.
  2. Converts them into float type (since input() returns strings by default).
  3. Adds the numbers and prints the result. ### Control Structures: If-Else Statements Control structures allow us to make decisions in our program. Consider this example:
age = int(input("Enter your age: "))

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote yet.")
Enter fullscreen mode Exit fullscreen mode

Here, the if statement checks if the user’s age is 18 or above and displays an appropriate message.


Loops in Python

Loops help execute repetitive tasks. The two main types are for and while loops.

Example of a for loop:

for i in range(5):
    print("Iteration:", i)
Enter fullscreen mode Exit fullscreen mode

Example of a while loop:

count = 0
while count < 5:
    print("Count:", count)
    count += 1
Enter fullscreen mode Exit fullscreen mode

Functions in Python

Functions allow you to reuse code efficiently. Here’s a simple function:

def greet(name):
    print("Hello, " + name + "!")

greet("Alice")
Enter fullscreen mode Exit fullscreen mode

Functions help keep your code organized and make it easier to debug.


Conclusion

This Python tutorial introduced you to the basics of Learn Python programming and guided you in writing your first program. From printing text to handling user input, using variables, loops, and functions, you’ve learned key Python concepts.

To continue learning, explore more Python tutorials, practice coding exercises, and start building small projects. Happy coding! 🚀

Top comments (1)

Collapse
 
typescriptonlinecompiler profile image
Typescript Online Compiler

this article is helpfull