Contents

Python Quick Guide - Step 1 Basic Syntax and Data Types (4) - Variables and Assignment

Info
  • 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.

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
Style Guide
  • Leave spaces before and after the assignment operator (=).
📚Exercise

Create a program that converts Celsius (℃) to Fahrenheit (°F).

  1. Assign 25 to a variable called celsius
  2. Convert it to Fahrenheit using the formula (celsius * 9/5) + 32
  3. Save the result to a variable called fahrenheit
  4. 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")

When naming variables, follow these rules:

  1. Use letters (a-z, A-Z), numbers, and underscores (_)
  2. Do not start with a number
  3. Do not use reserved words (if, for, while, etc.)
Style Guide
  • 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
  • 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.
  • 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 (==))
  • You can use these shorthand notations:
📚Exercise

Calculate the final price of an item that costs 5000 yen before tax, with a 20% discount and then a 10% tax added.

  1. Assign 5000 to a variable called total
  2. Apply a 20% discount
  3. Add 10% tax
  4. Display the final price as “The final payment amount is ○○ yen”

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
Traditional String Formatting Methods

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.

📚Exercise

Create a self-introduction using f-strings.

  1. Assign your name to a variable called name (e.g., “John Smith”)
  2. Assign your age to a variable called age (e.g., 25)
  3. Assign your hobby to a variable called hobby (e.g., “programming”)
  4. Assign your height in cm to a variable called height (e.g., 175.5)
  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

Related Content