Intro to Computer Science / Unit 4: Programming Basics in Python
4.2 Data Types
Notes
Every value has a data type. The main ones are:
| Type | Meaning | Example |
|---|---|---|
int | whole number | 42 |
float | number with a decimal point | 3.14 |
str | text in quotes | "hello" |
bool | True or False | True |
- Use
type(x)to see the type of a value. "5"(with quotes) is a string, not a number, so"5" + "5"gives"55".- Convert with
int(),float()andstr():int("7")gives7.
Practice
What is the type of the value
3.0?A number with a decimal point is a float, even if the decimal part is zero.
What does
print("4" + "6") display?Adding two strings joins them (concatenation), giving 46.
What does
print(int("12") + 3) display?int("12") is 12, and 12 + 3 = 15.
Ready for more? Take the Unit 4 test.