25:00
Focus
Lesson 2

Working with Simple Data Types

~6 min50 XP

Introduction

Welcome to the core of Python programming! Today, you will learn how to hold information inside your computer's memory using variables and display it using strings, forming the building blocks for every script you'll ever write.

Understanding Variables and Assignment

Think of a variable as a labeled box stored in your computer's memory. When you assign a value to it, you are effectively placing an object into that box so you can retrieve or use it later. In Python, you don't need to declare the type of data before using it; this is called dynamic typing. You simply choose a name and use the assignment operator (=) to store data.

Naming your variables effectively is crucial. Use descriptive, lowercase names that explain the purpose of the data, separating words with underscores (this is known as snake_case). For example, user_age is much better than just a or x. A common pitfall for beginners is using reserved keywords (like print, if, or while) as variable names, which prevents Python from performing its intended functions. Always ensure your variable names are unique and descriptive to keep your code readable and maintainable.

Exercise 1Multiple Choice
Which of the following is a valid, recommended variable name in Python?

Mastering Strings

A string is a sequence of characters enclosed in either single quotes ('...') or double quotes ("..."). Strings are used to store textual data, such as names, messages, or even formatted data like dates. Because strings are immutable, you cannot change an individual character once the string is created; instead, you create a new string if you need to modify the original.

You can combine strings using the + operator, which is called concatenation. However, be careful not to confuse numbers with strings. If you put quotes around a number, like '10', Python treats it as text, not a mathematical value. If you try to add a string to an integer, Python will raise a TypeError because it doesn't know how to "add" text to a digit without explicit conversion.

Using F-Strings for Formatting

While concatenation works, it quickly becomes messy and hard to read. Python provides a more modern, powerful tool called f-strings (formatted string literals). By placing the letter f before the opening quote of a string, you can insert variables directly into the text by placing them inside curly braces {}.

This approach is not only cleaner but also handles data types more gracefully. If you include an integer inside an f-string, Python automatically converts it to its text representation. This is the preferred way to inject data into messages in modern Python projects, helping you avoid the common headache of manually adding spaces and converting data types.

Exercise 2Fill in the Blank
To create a formatted string literal that injects a variable into text, you must prefix the string with the letter ___ before the opening quote.

Numeric Types: Integers and Floats

In Python, numbers are primarily categorized into integers (whole numbers) and floats (numbers with decimal points). Unlike strings, these types allow for mathematical operations. Understanding the difference is vital for precision. For example, dividing two integers in Python using / will always result in a float, even if the result is a whole number (e.g., 4/2=2.04 / 2 = 2.0).

If you need a whole number result from division, you use the floor division operator //. If you need the remainder of a division, you use the modulo operator %. Mastering these calculations is essential for everything from controlling game physics to calculating account balances.

Exercise 3True or False
In Python, the result of 10 / 2 is 5 (an integer).

Key Takeaways

  • Use snake_case for variable names and avoid Python keywords to keep your code clean and functional.
  • Strings are immutable sequences of text that can be joined using concatenation or, more effectively, via f-strings.
  • F-strings allow for cleaner, more readable code by embedding variables directly into strings using curly braces {}.
  • Python distinguishes between integers and floats, and performing operations between them typically results in a float to ensure numerical precision.
Check Your Understanding

Variables and descriptive naming conventions form the backbone of readable Python code. Explain why using snake_case and avoiding reserved keywords is important for someone else who might read your code later, and describe how you would choose an effective variable name if you were storing the price of a specific item in an online shop.

🔒Upgrade to submit written responses and get AI feedback
Go deeper
  • Why are variable names in snake_case preferred?🔒
  • What happens if I try to use a keyword as a name?🔒
  • Can I use both single and double quotes for strings?🔒
  • What does it mean for a string to be immutable?🔒
  • How can I modify a string if it is immutable?🔒