25:00
Focus
Lesson 1

Refresh Python Syntax and Variables

~10 min50 XP

Introduction

In this lesson, we will revisit the fundamental building blocks of Python, including syntax rules, dynamic typing, and variable assignment. Mastering these basics is the essential first step before we begin coding our professional-grade To-Do application.

Variables and Dynamic Typing

CPT-TheoryOfComp-Binary-Search-Python.png — MsDennig, CC BY-SA 4.0
CPT-TheoryOfComp-Binary-Search-Python.png — MsDennig, CC BY-SA 4.0

In Python, variables are essentially labels we create to store data. Unlike languages like C++ or Java, Python is dynamically typed, meaning you do not need to declare a variable's data type before assigning it. When you assign a value using the assignment operator (=), Python automatically determines the type based on the value provided.

For our To-Do application, we will store tasks as strings and completion statuses as booleans. Consider this example:

task_name = "Buy groceries"  # String
is_completed = False        # Boolean
priority_level = 1          # Integer

Note: Python variable names must start with a letter or an underscore and cannot start with a number. They are also case-sensitive, so task_name and Task_name are treated as two distinct variables. Always use descriptive names like task_list instead of x to keep your code readable.

Exercise 1Multiple Choice
Which of the following is a valid Python variable name declaration?

Data Types and Syntax

Python’s syntax is designed for readability, relying on indentation rather than curly braces to define blocks of code. When building our To-Do app, we will frequently work with collections like lists and dictionaries. A list is an ordered sequence of items, denoted by square brackets, which is perfect for storing our series of tasks.

# A list of tasks
tasks = ["Finish report", "Dentist appointment", "Call Mom"]

# Adding a task
tasks.append("Submit PR")

If you store an integer in a variable, you can perform arithmetic operations. For instance, if you want to track how many tasks are remaining, you might perform calculations directly within your logic: Remaining=TotalCompletedRemaining = Total - Completed

Exercise 2True or False
Python uses curly braces {} to define the scope of loops and conditional statements.

Working with Input and Output

CPT-TheoryOfComp-Binary-Search-Python.png — MsDennig, CC BY-SA 4.0
CPT-TheoryOfComp-Binary-Search-Python.png — MsDennig, CC BY-SA 4.0

To make our application interactive, we use the input() function to capture user data and the print() function to display information. When using input(), Python always treats the received data as a string. If you intend to take a number from the user—perhaps to select a task ID—you must use type casting, such as int().

Example of capturing a task from a user:

new_task = input("Enter your next task: ")
print(f"You have added: {new_task}")

The f-string notation (prefixing the string with f) allows you to embed variables directly inside the string using curly braces. This is the cleanest way to format output in your application.

Exercise 3Fill in the Blank
The ___ function is used to convert a string input from a user into a whole number format.

Key Takeaways

  • Use descriptive, case-sensitive names for variables and ensure they do not start with a digit.
  • Indentation is structurally mandatory in Python; it maintains the clarity and flow of your logic.
  • Always use type conversion (like int() or str()) when processing user data to ensure the variable type matches your intended operation.
Check Your Understanding

Understanding how Python handles data storage is critical for organizing tasks in our application. Imagine you are building a feature to track whether a task is finished and what its description is. Explain the difference between Python's dynamic typing and a statically typed language, and describe why using descriptive variable names like `task_description` instead of a generic name like `var1` is important for maintaining your To-Do application as it grows.

🔒Upgrade to submit written responses and get AI feedback
Go deeper
  • What happens if I change a variable's data type later?🔒
  • Can I use numbers inside a variable name?🔒
  • How does Python handle errors with variable naming?🔒
  • Are there specific indentation rules for lists?🔒
  • What is the benefit of using dictionaries over lists?🔒