Python Programming for Applied Projects
Python Basics
Storing Information
Think of a variable as a labeled box where you can store a piece of information. You give the box a name, and you put something inside it. In programming, this lets us save values to use later.
To create a variable in Python, you just pick a name, use the equals sign (), and provide the value you want to store. This process is called assignment.
# Storing a number
user_age = 25
# Storing some text
user_name = "Alex"
Here, user_age is a variable holding the number 25, and user_name is a variable holding the text "Alex". We can now use these names in our code instead of the actual values. Variable names are typically written in snake_case, using underscores to separate words.
Python's Building Blocks
Python handles different kinds of information in specific ways. The type of data determines what you can do with it. You can't do math on a word, for instance. Let's look at the most common data types.
integer
noun
A whole number, positive or negative, without any decimal points.
Integers, or int, are for whole numbers. Floats, or float, are for numbers that have a fractional part.
# An integer
items_in_cart = 10
# A float
price_per_item = 4.99
Next up is text, which programmers call strings.
string
noun
A sequence of characters, such as letters, numbers, or symbols, used to represent text. In Python, strings are enclosed in single (' ') or double (" ") quotes.
Strings, or str, are how you store any text.
greeting = "Hello, Python!"
Finally, we have Booleans. They are simple, but powerful.
boolean
noun
A data type that has one of two possible values, typically denoted as true or false.
A Boolean, or bool, can only have one of two values: True or False. Note the capital letters. They are often used to represent the state of something, like whether a light is on or off.
is_logged_in = True
has_errors = False
Interacting with the User
Your programs can communicate with the user. The print() function is the standard way to display output. Whatever you put inside the parentheses will be shown to the user.
# Printing a string directly
print("Welcome to the program!")
# Printing the value of a variable
user_name = "Alex"
print(user_name)
To get information from the user, you can use the input() function. It shows the user a message, waits for them to type something, and then returns whatever they typed as a string.
# Ask the user for their name
name = input("What is your name? ")
# Greet the user with their name
print("Hello, " + name)
In this example, the program will display "What is your name? ", pause, and store the user's entry in the name variable. Then, it uses that variable to print a personalized greeting.
An important detail: input() always gives you back a string, even if the user types a number. If you need to treat the input as a number, you'll have to convert it first, which we'll cover later on.
What is the primary purpose of a variable in programming?
Which line of code correctly assigns the number 50 to a variable named player_score?
With these basics, you can start writing simple programs that store information and interact with users.