Understanding Strings, Integers, and Floats
In this tutorial, you'll learn about strings, integers, and floats—the three most common types of data in Python. By the end of this lesson, you'll know how to use them in programs and when to use each type.
In this tutorial, you'll learn about strings, integers, and floats—the three most common types of data in Python. By the end of this lesson, you'll know how to use them in programs and when to use each type.
1. What Are Strings, Integers, and Floats?
Strings (str)
A string is text enclosed in quotation marks (" or ').
Strings can include letters, numbers, spaces, and symbols.
Example: "Hello", 'Python123', "What's up?"
Integers (int)
An integer is a whole number (positive, negative, or zero).
Example: 10, -5, 0
Floats (float)
A float is a number with a decimal point.
Example: 3.14, -0.5, 100.0
2. How to Use Strings, Integers, and Floats in Python
a. Strings
You can use strings to store and manipulate text.
Example:
name = "Alice"
print("Hello, my name is", name)
Output:
Hello, my name is Alice
You can also combine strings using the + operator:
greeting = "Good"
time = "morning"
print(greeting + " " + time)
Output:
Good morning
b. Integers
You can use integers for math and counting.
Example:
age = 12
print("I am", age, "years old.")
Output:
I am 12 years old.
You can perform math operations:
apples = 5
oranges = 3
total = apples + oranges
print("Total fruits:", total)
Output:
Total fruits: 8
c. Floats
Use floats for numbers with decimals.
Example:
price = 3.99
print("The price is", price)
Output:
The price is 3.99
You can also do math with floats:
radius = 2.5
area = 3.14 * (radius ** 2)
print("The area of the circle is:", area)
Output:
The area of the circle is: 19.625
3. Combining Strings, Integers, and Floats
You can mix strings, integers, and floats in one program.
Example:
item = "notebook"
quantity = 3
price_per_item = 1.5
total_price = quantity * price_per_item
print("You bought", quantity, item + "(s) for a total of $", total_price)
Output:
You bought 3 notebook(s) for a total of $ 4.5
4. Practice Exercises
Exercise 1: String Practice
Create a program that:
Stores your favorite color in a string.
Prints "My favorite color is [color]."
Exercise 2: Integer Practice
Write a program that:
Stores the number of books you have.
Prints "I have [number] books."
Exercise 3: Float Practice
Write a program that:
Stores the price of your favorite snack.
Prints "My favorite snack costs $[price]."
Exercise 4: Combining Data Types
Write a program that:
Stores your name (string), age (integer), and height in feet (float).
Prints a sentence like this:
My name is Alex. I am 13 years old, and I am 5.5 feet tall.
5. Bonus Challenge
Write a program that calculates the total cost of a shopping list:
Create variables for three items (name and price).
Calculate the total cost.
Print the result in a sentence like this:
You bought a pen ($1.5), a notebook ($2.0), and an eraser ($0.5).
The total cost is $4.0.
6. What’s Next?
Once you’re comfortable using strings, integers, and floats, you can explore more complex data types like lists and dictionaries. Great work getting started with Python! 🎉