Remember
An object has a type that determines which operations it supports. The name itself is not locked to that type.
An object has a type that determines which operations it supports. The name itself is not locked to that type.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)Top-level scope
'3'An object has a type that determines which operations it supports. The name itself is not locked to that type.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)
|
v
DISPLAYED RESULT
int
str
FINAL BINDINGS
value --> object 2 (str)
object 2: '3'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)Top-level scope
'3'The example has finished. Compare the final output with the values and references above.
int str
What do you expect this program to print?
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)In Python, data comes in different types. Just like in real life, where you have different kinds of things — like numbers, words, and yes/no answers — Python also needs to know what kind of data you're using.
Python uses data types to decide what you can do with the data. For example, you can add two numbers, but you can’t add a number to a word directly.
Here are some of the most common data types you'll use in Python language:
5, -2, 100)3.14, -0.5, 2.0)"Hello", "Python")number = 10 # int
pi = 3.14 # float
name = "Alice" # str
is_happy = True # bool
You can use the type() function to see what kind of data something is:
number = 10 # int
pi = 3.14 # float
name = "Alice" # str
is_happy = True # bool
print(type(10)) # <class 'int'>
print(type("Hi")) # <class 'str'>
print(type(3.5)) # <class 'float'>
print(type(False)) # <class 'bool'>
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>
Every value in Python has a type. Understanding these basic data types will help you store, use, and work with information in your programs.
Connect what you noticed in the example with the idea below.
An object has a type that determines which operations it supports. The name itself is not locked to that type.
A variable must keep the type of its first value.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.