Python Quick Guide - Step 1 Basic Syntax and Data Types (4) - Variables and Assignment
- This course is designed to help you learn the basics of Python programming as quickly as possible through hands-on practice.
- The “Style Guide” sections primarily cover guidelines from PEP8 for writing clean Python code.
- You can run and see the results of each code example.
Feel free to experiment with the code - reloading the page will reset the content.
Step 1 Basic Syntax and Data Types
1.4. Variables and Assignment
1.4.1. What are Variables
Variables are fundamental elements in programming that serve as “named containers” for temporarily storing data.
- A variable refers to a location in memory that stores a value
- Saving a value to a variable is called assignment
- For assignment, the variable goes on the left side of the equals sign (
=
), and the value goes on the right
- Leave spaces before and after the assignment operator (
=
).
Create a program that converts Celsius (℃) to Fahrenheit (°F).
- Assign 25 to a variable called
celsius
- Convert it to Fahrenheit using the formula
(celsius * 9/5) + 32
- Save the result to a variable called
fahrenheit
- Display both temperatures like “25℃ is 77.0°F”
-
First try displaying it using the methods we’ve already covered
-
Then try displaying it using the following format (which we’ll cover in section 1.4.4):
print(f"{celsius}℃ is {fahrenheit}°F")
-
1.4.2. Variable Naming Rules
When naming variables, follow these rules:
- Use letters (
a-z
,A-Z
), numbers, and underscores (_
) - Do not start with a number
- Do not use reserved words (
if
,for
,while
, etc.)
- Variable names should use lowercase letters with words separated by underscores (
_
).
- Choose variable names that clearly indicate their purpose
- Avoid overly short names (
i
,j
, etc.) - Particularly avoid lowercase L (
l
), uppercase O (O
), and uppercase I (I
) as they have poor visibility
- Avoid overly short names (
- Non-English variable names are possible but not recommended
- Variables whose values don’t change during a program are called constants.
For constants, it’s recommended to use all uppercase letters with words separated by underscores (_
).- In reality, Python doesn’t have a feature to declare “unchangeable constants.”
Uppercase variable names are a convention among developers meaning “this value shouldn’t be changed,”
but technically, they can be modified just like regular variables.
- In reality, Python doesn’t have a feature to declare “unchangeable constants.”
1.4.3. Variable Reassignment
- Variable values can be changed.
- Variables can also be reassigned to values of different types.
- You can reassign a variable using its current value.
- This may be confusing if you think of the equals sign (
=
) as in mathematics,
but in programming, the equals sign represents the “assignment” operation.
(As we’ll see later, “equality” is represented by double equals (==
))
- This may be confusing if you think of the equals sign (
- You can use these shorthand notations:
Calculate the final price of an item that costs 5000 yen before tax, with a 20% discount and then a 10% tax added.
- Assign 5000 to a variable called
total
- Apply a 20% discount
- Add 10% tax
- Display the final price as “The final payment amount is ○○ yen”
1.4.4. Using Variables: String Formatting (f-strings)
As you’ve seen in the exercises above, there are times when you want to embed variable values within strings.
Python’s f-strings make this easy.
- Create an f-string by adding
f
before the string - Use variables inside the string by writing
{variable_name}
- f-strings can include not just variables but also the results of calculations
- When displaying
float
values, you can specify the number of decimal places
- You can also pad numbers with zeros
- You can align text right, left, or center with specified width
- f-strings have many other features. For more details, please refer to:
f-strings are a relatively new feature introduced in Python 3.6.
Previously, the following methods were used:
f-strings are more readable and concise than these methods, so we recommend using them whenever possible.
Create a self-introduction using f-strings.
- Assign your name to a variable called
name
(e.g., “John Smith”) - Assign your age to a variable called
age
(e.g., 25) - Assign your hobby to a variable called
hobby
(e.g., “programming”) - Assign your height in cm to a variable called
height
(e.g., 175.5) - Using f-strings, display all this information in one line:
- “My name is ○○. I am ○○ years old, and my hobby is ○○. My height is ○○ cm.”
- Display the height with 1 decimal place