Python Basics
Learn the fundamentals of Python programming: print statements, variables, data types, and comments.
Your First Python Program
Python is one of the most popular programming languages in the world. It is known for its clean, readable syntax that makes it an excellent first language to learn. Let's start with the most fundamental command: print().
The print() function displays output to the screen. It is the simplest way to see what your program is doing.
# Your very first Python program
print("Hello, World!")
print("Welcome to Python programming!")
print("My name is BrightPath Student")
Hello, World!Welcome to Python programming!My name is BrightPath Student
Variables and Data Types
A variable is like a labelled box that stores a value. In Python, you create a variable simply by giving it a name and assigning a value with the = sign. Python automatically detects the data type.
Python has four basic data types you need to know:
int
Whole numbers: 42, -7, 0
float
Decimal numbers: 3.14, -0.5, 100.0
str
Text in quotes: "Hello", 'Python'
bool
True or False: True, False
# Creating variables with different data types name = "Alice" # str (string) age = 16 # int (integer) height = 1.65 # float (decimal) is_student = True # bool (boolean) # Printing variables print(name) print(age) print(type(age)) # Shows the data type
Alice16<class 'int'>
Comments in Python
Comments are notes you write in your code for humans to read. Python ignores them completely when running the program. They help explain what your code does and why.
# This is a single-line comment
print("This runs") # Comments can go after code too
# You can use comments to temporarily disable code:
# print("This line will NOT run")
"""
This is a multi-line comment (docstring).
It can span several lines.
Useful for longer explanations.
"""
Key Vocabulary
Variable
A named container that stores a value in the computer's memory, like age = 16.
Data Type
The kind of value stored: int (whole number), float (decimal), str (text), or bool (True/False).
print()
A built-in function that displays output to the screen so you can see results.
Comment
A note in your code (starting with #) that Python ignores. Used to explain your code.
Worked Examples
Create variables to store a student's details and print them
# Store student information
student_name = "Jordan"
student_age = 16
student_grade = 11.5
is_enrolled = True
# Display the information
print("Name:", student_name)
print("Age:", student_age)
print("Grade:", student_grade)
print("Enrolled:", is_enrolled)
Step 1: We create four variables, each with a different data type.
Step 2: student_name is a str, student_age is an int, student_grade is a float, and is_enrolled is a bool.
Output: Name: Jordan | Age: 16 | Grade: 11.5 | Enrolled: True
Use type() to check data types
score = 95 average = 87.5 subject = "Maths" passed = True print(type(score)) # <class 'int'> print(type(average)) # <class 'float'> print(type(subject)) # <class 'str'> print(type(passed)) # <class 'bool'>
Step 1: We assign values of different types to variables.
Step 2: The type() function returns the data type of any value or variable.
Basic arithmetic with variables
# Calculate the total cost
price = 12.50
quantity = 4
total = price * quantity
print("Total cost: $", total)
# Update a variable
total = total + 5.00 # Add $5 delivery fee
print("Total with delivery: $", total)
Step 1: We create price and quantity, then calculate total using multiplication.
Step 2: Variables can be updated by reassigning them with a new value.
Output: Total cost: $ 50.0 | Total with delivery: $ 55.0
Knowledge Check
Select the correct answer for each question. Click "Check Answer" to see if you are right.
Question 1
What will print("Hello" + " " + "World") output?
Question 2
What is the data type of the value 3.14?
Question 3
Which of the following is a valid Python variable name?
Question 4
What does the # symbol do in Python?
Question 5
What will type(True) return?
Key Concepts Summary
-
●
print()displays output to the screen. -
●
Variables store values using the
=assignment operator. - ● Python has four basic data types: int, float, str, and bool.
-
●
Use
type()to check what data type a value is. -
●
Comments start with
#and are ignored by Python.