Introduction to Python Programming
Python Basics
Your First Steps in Python
Python is known for its clean and readable style. It's designed to look a lot like plain English, which makes it one of the easiest programming languages to learn. The most basic action you can perform is displaying information. For this, we use a built-in command called print().
You just write print() and put whatever you want to display inside the parentheses. If you're printing text, you need to wrap it in quotation marks.
print("Hello, World!")
This simple line of code is a classic first step in programming. It tells the computer to display the text "Hello, World!" on the screen. It's a small command, but it's your first instruction to the machine.
Storing Information with Variables
Constantly retyping information is inefficient. Instead, we can store data in variables. Think of a variable as a labeled box where you can keep a piece of information. You give the box a name, and you can see what's inside or change its contents later.
To create a variable, you choose a name and use the equals sign (=) to assign it a value. This process is called assignment.
# Assign the string "Hello, Python!" to a variable named 'greeting'
greeting = "Hello, Python!"
# Print the value stored in the variable
print(greeting)
Here, greeting is the variable's name. The computer now knows that greeting holds the text "Hello, Python!". When we ask it to print(greeting), it looks inside that box and prints the content.
Python's Data Types
Variables can hold different kinds of information, not just text. These different kinds are called data types. For now, we'll focus on four of the most common ones: integers, floats, strings, and booleans.
| Data Type | Description | Example |
|---|---|---|
| Integer | Whole numbers | 42, -10, 0 |
| Float | Numbers with a decimal part | 3.14, -0.01 |
| String | Textual data | "Alice", 'cat' |
| Boolean | Represents True or False | True, False |
A string is just text, enclosed in single (') or double (") quotes. An integer is a whole number, positive or negative. A float, short for floating-point number, is a number that has a decimal point. Finally, a boolean can only have one of two values: True or False. It's used to represent logical states, like whether a light switch is on or off.
# An integer
student_count = 25
# A float
price = 99.95
# A string
subject = "Computer Science"
# A boolean
is_enrolled = True
print(student_count)
print(price)
print(subject)
print(is_enrolled)
Working with Data: Operators
Once you have data, you need to be able to work with it. Operators are special symbols that perform operations on values and variables. Let's start with the ones you already know from math class.
Arithmetic operators perform mathematical calculations. Python uses
+for addition,-for subtraction,*for multiplication, and/for division.
apples = 10
oranges = 5
# Addition
print(apples + oranges) # Output: 15
# Multiplication
print(apples * 2) # Output: 20
Next up are comparison operators. These are used to compare two values, and the result is always a boolean (True or False).
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 10 | True |
> | Greater than | 10 > 5 | True |
< | Less than | 10 < 5 | False |
>= | Greater than or equal to | 5 >= 5 | True |
<= | Less than or equal to | 10 <= 5 | False |
Notice the double equals sign (
==) for checking equality. A single equals sign (=) is for assignment, a very different action.
Finally, logical operators (and, or, not) are used to combine boolean values.
andisTrueonly if both sides are true.orisTrueif at least one side is true.notinverts a boolean value.
has_coffee = True
is_tired = False
# Both conditions must be True
print(has_coffee and is_tired) # Output: False
# Only one condition needs to be True
print(has_coffee or is_tired) # Output: True
# Inverts the value of is_tired (False becomes True)
print(not is_tired) # Output: True
Let's test what you've learned about Python's fundamental building blocks.
What is the primary command used in Python to display information on the screen?
Which of the following correctly assigns the text 'Hello' to a variable named greeting?