Every programmer's journey begins with the classic "Hello, World!" program. Let's start with the simplest Python program you can write:
example:
print("Hello, World!")
When you run this program, it will display:
Hello, World!
The print()
function is used to display text on the screen. It's one of the most commonly used functions in Python.
Python provides an interactive console (also called REPL - Read-Eval-Print Loop) where you can test code immediately. You can access it by typing python
or python3
in your terminal.
example:
>>> print("Welcome to Python!")
Welcome to Python!
>>> 2 + 3
5
>>> name = "Sarah"
>>> print(name)
Sarah
The >>>
symbols indicate you're in the Python interactive mode. This is perfect for experimenting and learning.
While the console is great for testing, you'll want to save your code in files called scripts. Python files have a .py
extension.
Create a file called my_first_script.py
:
example:
# My first Python script
print("Welcome to my Python program!")
print("Today is a great day to learn programming.")
# Let's do some basic math
result = 10 + 5
print("10 + 5 equals:", result)
To run this script, save it and use:
python my_first_script.py
Variables are like containers that store data. In Python, creating variables is straightforward:
example:
# Storing different types of information
student_name = "Emily"
student_age = 20
student_grade = 85.5
is_enrolled = True
print("Student Name:", student_name)
print("Age:", student_age)
print("Grade:", student_grade)
print("Enrolled:", is_enrolled)
Notice how we don't need to specify what type of data each variable holds - Python figures it out automatically!
Python has several built-in data types. Let's explore the most common ones:
example:
# Numbers
temperature = 23 # Integer (whole number)
price = 19.99 # Float (decimal number)
# Text
message = "Hello there!" # String (text)
letter = 'A' # Also a string (single character)
# True/False values
is_sunny = True # Boolean
is_raining = False # Boolean
# Let's check their types
print(type(temperature)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(message)) # <class 'str'>
print(type(is_sunny)) # <class 'bool'>
Making your programs interactive is exciting! Use the input()
function to get information from users:
example:
# Getting user information
name = input("What's your name? ")
age = input("How old are you? ")
print("Nice to meet you,", name)
print("You are", age, "years old.")
Important note: The input()
function always returns text (string), even if the user types numbers.
example:
# Converting input to numbers
age_text = input("Enter your age: ")
age_number = int(age_text) # Convert to integer
print("Next year you'll be", age_number + 1, "years old!")
Python excels at mathematical operations. Let's create a simple calculator:
example:
# Basic math operations
num1 = 15
num2 = 4
addition = num1 + num2 # 19
subtraction = num1 - num2 # 11
multiplication = num1 * num2 # 60
division = num1 / num2 # 3.75
floor_division = num1 // num2 # 3 (whole number division)
remainder = num1 % num2 # 3 (remainder after division)
power = num1 ** 2 # 225 (15 squared)
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Floor Division:", floor_division)
print("Remainder:", remainder)
print("Power:", power)
Strings (text) are fundamental in programming. Here are some basic string operations:
example:
first_name = "John"
last_name = "Smith"
# Combining strings (concatenation)
full_name = first_name + " " + last_name
print("Full name:", full_name)
# String length
print("Name length:", len(full_name))
# Making strings uppercase and lowercase
print("Uppercase:", full_name.upper())
print("Lowercase:", full_name.lower())
# Using f-strings (modern way to format strings)
age = 25
message = f"Hello, my name is {full_name} and I'm {age} years old."
print(message)
Let's combine everything we've learned to create a simple interactive program:
example:
# Personal Information Collector
print("=== Welcome to the Personal Info Collector ===")
print()
# Get user information
name = input("Enter your full name: ")
age = input("Enter your age: ")
city = input("Enter your city: ")
hobby = input("Enter your favorite hobby: ")
# Convert age to number for calculation
age_number = int(age)
birth_year = 2025 - age_number
# Display the collected information
print("\n=== Your Information ===")
print(f"Name: {name}")
print(f"Age: {age} years old")
print(f"Birth Year: {birth_year}")
print(f"City: {city}")
print(f"Favorite Hobby: {hobby}")
print(f"Fun Fact: In 10 years, you'll be {age_number + 10} years old!")
print("\nThank you for using our program!")
# Wrong
name = John # This will cause an error
# Correct
name = "John"
# Be careful with spelling
student_name = "Alice"
print(studnet_name) # Typo! This will cause an error
# This won't work as expected
age = input("Enter age: ")
next_year = age + 1 # Error! Can't add number to string
# Correct way
age = int(input("Enter age: "))
next_year = age + 1
Try these exercises to reinforce what you've learned:
Exercise 1: Simple Calculator
# Create a program that asks for two numbers and displays their sum, difference, and product
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Your code here...
Exercise 2: Temperature Converter
# Convert Celsius to Fahrenheit
# Formula: F = (C × 9/5) + 32
celsius = float(input("Enter temperature in Celsius: "))
# Your code here...
Exercise 3: Personal Story Generator
# Create a mad-libs style story using user input
name = input("Enter a name: ")
adjective = input("Enter an adjective: ")
animal = input("Enter an animal: ")
# Create and print a fun story using these variables
Congratulations! You've taken your first steps in Python programming. You now know how to:
In the next lessons, you'll learn about:
- Variables and Data Types (in more detail)
- Control Flow (making decisions with if/else)
- Loops (repeating actions)
- Data Structures (lists, dictionaries, etc.)
Keep practicing these basics - they form the foundation for everything else in Python programming!
Essential Functions:
- print()
- Display output
- input()
- Get user input
- type()
- Check data type
- int()
- Convert to integer
- float()
- Convert to decimal number
- str()
- Convert to string
- len()
- Get length of string
Basic Operators:
- +
Addition
- -
Subtraction
- *
Multiplication
- /
Division
- //
Floor division
- %
Remainder
- **
Power