Mastering Python
Python Basics
Your First Lines of Code
Let's start by writing a simple program. In programming, a common tradition is to make your first program display the text "Hello, World!". In Python, this is incredibly straightforward.
print("Hello, World!")
That's it. This one line tells the computer to display the text inside the parentheses. The print() part is a function, which is a reusable block of code that performs a specific action. Here, its action is to print output to the screen.
Notice the clean look. Python doesn't require semicolons at the end of lines like many other languages. It values readability. What is crucial, however, is indentation—the spaces at the beginning of a line. We'll see why that's so important a bit later.
Storing Information
Programs need to work with information, like text, numbers, and dates. To keep track of this information, we use variables. Think of a variable as a labeled box where you can store a piece of data.
# Assigning the text "Alice" to a variable named 'user_name'
user_name = "Alice"
# Assigning the number 30 to a variable named 'user_age'
user_age = 30
# Now we can print the variables
print(user_name)
print(user_age)
Here, user_name and user_age are our variables. The equals sign (=) is the assignment operator; it assigns the value on the right to the variable on the left.
Python automatically figures out what type of data you're storing. Let's look at the most common types.
string
noun
A sequence of characters, used to represent text. Strings are enclosed in either single quotes (' ') or double quotes (" ").
integer
noun
A whole number, without a fractional part. It can be positive, negative, or zero.
float
noun
A number that contains a decimal point. The name comes from "floating-point number."
You can also get information from a user. The input() function pauses the program and waits for the user to type something and press Enter.
name = input("What is your name? ")
print("Hello, " + name)
One important detail: input() always gives you back a string. If you need a number, you have to convert it.
age_string = input("How old are you? ")
age_number = int(age_string) # Convert the string to an integer
years_until_100 = 100 - age_number
print("You will be 100 in", years_until_100, "years.")
Making Decisions
Programs often need to make decisions and run different code depending on the situation. This is handled by control structures. The most basic is the if statement. It checks if a condition is true and, if so, runs a block of code.
temperature = 75
if temperature > 70:
print("It's a warm day!")
The indented code under the if line only runs when the condition temperature > 70 is true.
You can handle multiple conditions using elif (short for "else if") and else.
age = 17
if age >= 18:
print("You are eligible to vote.")
elif age >= 16:
print("You can get a driver's license.")
else:
print("You are a minor.")
In Python, indentation is not just for style; it's syntax. It defines which lines of code belong to a control structure like an
ifstatement or a loop.
Repeating Actions
Sometimes you need to repeat an action multiple times. That's where loops come in.
A for loop is used to iterate over a sequence of items. For example, you can use range() to generate a sequence of numbers to loop through.
# This loop will run 5 times, for numbers 0 through 4
for number in range(5):
print("Attempt number", number)
A while loop continues to run as long as its condition remains true.
countdown = 3
while countdown > 0:
print(countdown)
countdown = countdown - 1 # Decrease the value by 1
print("Liftoff!")
It's crucial to make sure the condition for a while loop will eventually become false. Otherwise, you'll create an infinite loop!
Now that you've got the basics down, let's test your understanding.
Which line of code correctly prints the text "Hello, World!" to the screen in Python?
What is the primary purpose of a variable in programming?
With variables, control structures, and loops, you have the fundamental tools to start building simple yet powerful programs in Python.