Get started with Python programming fundamentals
Welcome to Python programming! Python is one of the most popular and versatile programming languages in the world. It's used for web development, data science, artificial intelligence, automation, and much more. In this tutorial, you'll learn the fundamental building blocks of Python programming.
💡 Tip: Python is known for its simple, readable syntax. It's perfect for beginners and powerful enough for experts!
Let's start with the classic "Hello, World!" program. This simple program displays text on the screen.
# This is your first Python program
print("Hello, World!")
To run this program:
hello.pypython hello.py in your terminalVariables are containers for storing data values. In Python, you don't need to declare variable types explicitly - Python figures it out automatically!
# String variable
name = "Alice"
# Integer variable
age = 25
# Float variable
height = 5.6
# Boolean variable
is_student = True
# Print variables
print(name)
print(age)
print(height)
print(is_student)
"Hello"423.14True💡 Tip: You can check a variable's type using the type() function: type(name)
Python supports various operators for performing calculations and comparisons.
# Addition
result = 10 + 5 # 15
# Subtraction
result = 10 - 5 # 5
# Multiplication
result = 10 * 5 # 50
# Division
result = 10 / 5 # 2.0
# Floor Division
result = 10 // 3 # 3
# Modulus (remainder)
result = 10 % 3 # 1
# Exponentiation
result = 2 ** 3 # 8
x = 10
y = 5
print(x > y) # True (greater than)
print(x < y) # False (less than)
print(x == y) # False (equal to)
print(x != y) # True (not equal to)
print(x >= y) # True (greater than or equal)
print(x <= y) # False (less than or equal)
Python makes it easy to get input from users and display output.
# Ask user for their name
name = input("What is your name? ")
print("Hello, " + name + "!")
# Ask for age (convert to integer)
age = int(input("How old are you? "))
print("You are " + str(age) + " years old.")
⚠️ Note: The input() function always returns a string. Use int() or float() to convert to numbers.
Python offers multiple ways to format strings:
name = "Alice"
age = 25
# Method 1: Concatenation
print("My name is " + name + " and I am " + str(age))
# Method 2: f-strings (recommended)
print(f"My name is {name} and I am {age}")
# Method 3: format() method
print("My name is {} and I am {}".format(name, age))
Comments are notes in your code that Python ignores. They help you and others understand your code.
# This is a single-line comment
"""
This is a multi-line comment.
It can span multiple lines.
Use it for longer explanations.
"""
x = 5 # You can also add comments after code
Now it's your turn! Try creating a simple program that:
💡 Challenge: Try to use f-strings for formatting your output!
# Get user input
name = input("What is your name? ")
fav_number = int(input("What is your favorite number? "))
# Calculate
doubled = fav_number * 2
# Display result
print(f"Hello {name}! Your favorite number is {fav_number}.")
print(f"When doubled, it becomes {doubled}!")
Congratulations! You've learned the basics of Python programming. Here's what we covered:
print()input()Now that you understand Python basics, you're ready to learn about:
💡 Keep Practicing: The best way to learn programming is by writing code. Try modifying the examples and creating your own programs!
Now that you understand Python basics, let's create your first real project! This hands-on exercise will help you practice everything you've learned.
Test your understanding of Python basics!