Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Let's pull the whole chapter together. In one sentence, what is a variable?
A name that refers to a value stored in memory, like age = 25.
Right. And the = sign means 'store this value under this name', not 'is equal to'. So what does age = age + 1 do?
It works out age + 1, which is 26, and then stores 26 back in age.
Exactly. Which data types did we meet in this chapter?
int, float, str and bool, plus NoneType for the value None.
Good. Now a tricky one: is '25' an int?
No, the quotes make it a string, so type('25') shows <class 'str'>.
Values often arrive as text, for example from a form. Suppose quantity = '12'. What is quantity * 2?
24?
Try it and you'll get '1212', because multiplying a string repeats it. What should you do first?
Convert it with int(quantity). Then int(quantity) * 2 gives 24.
What about int('3.5')? Does that give 3?
No, that raises a ValueError, because '3.5' isn't written as a whole number. Use float('3.5'), or int(float('3.5')) if you really want 3. And remember, int() drops the decimal part; it doesn't round.
And what does bool() do with text?
An empty string gives False and any other string gives True, even the string 'False'. Zero, 0.0 and None are False too.
Last topic: names. Which of these are valid: _total, 2nd_place, user-name, userName and class?
_total and userName are valid. 2nd_place starts with a digit, and user-name has a hyphen.
And class is a keyword, so Python won't accept it as a name.
Perfect. Don't forget that names are case-sensitive, so score and Score are two different variables.
Which style should we use, userName or user_name?
Both work, but Python's style guide, PEP 8, recommends lowercase snake_case like user_name. And pick names that say what the value means: price beats p.
A name bound to a value with =; it can be reassigned at any time.
int, float, str, bool and NoneType, and the quotes, not the digits, decide whether a value is text.
Reports a value's type and is the fastest way to catch a number that is really a string.
int(), float(), str() and bool() change a value's type; int() drops decimals and fails on text that is not a whole number.
Start with a letter or underscore, use only letters, digits and underscores, remember that case matters, and avoid keywords.
The type belongs to the value, so the same name can later refer to a value of a different type.
Store one value of each core type and print it with its type:
city = "Pune"
population = 3124458
temperature = 31.5
is_raining = False
forecast = None
print(city, type(city))
print(population, type(population))
print(temperature, type(temperature))
print(is_raining, type(is_raining))
print(forecast, type(forecast))Convert text to numbers before doing arithmetic, as in the chapter's "23" exercise:
age_text = "23"
print(age_text * 2)
age = int(age_text)
print(age * 2)
print(float("3.5") + 1)
print(int(3.9))
print(str(age) + " years")See which values bool() treats as False and which as True:
print(bool(0), bool(0.0), bool(""), bool(None))
print(bool(7), bool(-1), bool("hello"), bool("False"))Reassign one variable and watch its type change (dynamic typing):
answer = 42
print(answer, type(answer))
answer = "forty-two"
print(answer, type(answer))
answer = None
print(answer, type(answer))Quotes around a number turn it into text; call type() to check it before you do what's next.
Asha's shop program read a quantity of "3" from a form and multiplied it by 3, and the bill said 333 instead of 9. She printed type(quantity), saw it was a str, wrapped it in int(), and the bill came out right.
I Feel Safe Being Normal - Int, Float, Str, Bool, NoneType, the five core types.
L.U.C.K. - Letter or underscore first, Use only letters, digits and underscores, Case matters, Keywords are banned.
Term
What does the `=` sign do in `score = 10`?
Click to flip
Term
Name the five core data types from this chapter.
Click to flip
Term
What does `print(type("25"))` display?
Click to flip
Term
What is the result of `int(3.9)`?
Click to flip
Term
Why does `int("hello")` fail?
Click to flip
Term
Which values does `bool()` turn into False?
Click to flip
A name that refers to a value stored in the computer's memory.
Storing a value under a name with the = operator, as in age = 25.
The kind of value something is, such as int, float, str, bool or NoneType, which decides what you can do with it.
The type of the special value None, which means "no value yet".
A built-in function that returns the type of a value; printing the result shows, for example, <class 'int'>.
Turning a value into another type with int(), float(), str() or bool().
Python's rule that a type belongs to the value, not the name, so a variable can be reassigned to a value of another type.
A word reserved by Python, such as if, for or True, that cannot be used as a variable name.
This summary brings Chapter 1 together: variables as names for values, the five core data types (int, float, str, bool and NoneType), checking a value's type with type(), converting between types, and the rules for naming variables.
A variable is a name that refers to a value. You create it with the assignment operator = and can reassign it at any time, even to a value of a different type. Python's core types are int, float, str, bool and NoneType. The built-in type() function tells you which type a value has, and int(), float(), str() and bool() convert between types, which matters most when numbers arrive as text. Valid names start with a letter or underscore, contain only letters, digits and underscores, are case-sensitive and cannot be keywords. This summary reviews each idea, points out the mistakes beginners make most often, and finishes with a short program that uses everything together.
Every program works with data, and Chapter 1 gave you two tools for holding it: variables, which give a value a name, and data types, which say what kind of value it is. Python reads name = value as "store this value under this name". The = sign is an instruction, not a claim that two things are equal, so count = count + 1 makes perfect sense: Python works out the right side first, then stores the result under the name on the left.
The quotes decide the type, not the digits: 25 is an int, but "25" is a str. Likewise 24.0 is a float because of its decimal point, even though it is a whole number.
type(value) returns the type of a value, so print(type(age)) shows <class 'int'>. Use it whenever a result surprises you. Many beginner bugs come from a number that is secretly a string: "12" * 2 gives "1212", not 24.
int("23") gives 23, float("3.5") gives 3.5 and str(25) gives "25".int(3.9) gives 3. Converting a float to an int drops the decimal part; it does not round.int("3.5") and int("hello") raise a ValueError, because the text is not a whole number. Convert in two steps instead: int(float("3.5")) gives 3.bool() returns False for 0, 0.0, the empty string "" and None, and True for almost everything else, including the non-empty string "False".1name is invalid.user-name is invalid because - means minus, and my name is invalid because of the space.age, Age and AGE are three different variables.if, else, for, True and None are reserved and cannot be used as names.total_score, and choose names that describe the value.The program below stores one value of each type, prints each with its type, and shows why converting text matters before doing arithmetic.
fruit = "Mango"
quantity = 12
price = 0.75
in_stock = True
discount = None
print(fruit, type(fruit))
print(quantity, type(quantity))
print(price, type(price))
print(in_stock, type(in_stock))
print(discount, type(discount))
quantity_text = "12"
print("Text times 2:", quantity_text * 2)
print("Number times 2:", int(quantity_text) * 2)
print("Total cost:", quantity * price)Multiplying the string "12" by 2 repeats the text and prints 1212, while converting it with int() first gives the real answer, 24.