Master Python data types with this comprehensive guide. Learn about numeric, string, boolean, and collection data types with examples, exercises, and tasks. Perfect for beginners and professionals to enhance their Python programming skills.
Incorrect Input Conversion [Python Quiz #64]
age = input("Enter your age: ")
result = age * 2
Answer Key (True/False):
What is the output of the following code? [Python Quiz #22]
age = 25
print("I am " + str(age) + " years old.")
Watch this video for the answer:https://youtube.com/shorts/DBC5-ZYoGXI?si=PXk-CPGymx2Q6X2p
age = 25
message = "You are " + str(age) + " years old."
message += " Welcome!"
print(message)
type(3 + 4.0)
- A) <class 'str'>
- B) <class 'bool'>
- C) <class 'int'>
- D) <class 'float'>
x = 10
x = "Hello"
print(type(x))
- A) <class 'int'>
- B) <class 'str'>
- C) <class 'bool'>
- D) <class 'float'>
x = 5
x = 5.0
x = True
x = "Python"
print(x)
- A) 5
- B) 5.0
- C) True
- D) Python
x = "10"
x = int(x) + 2
print(x)
- A) "102"
- B) 102
- C) 12
- D) "12"
Answer Key (Fill in the Blanks):
5
to a variable named x
.10
to a variable named y
.x
and y
to a variable named sum_xy
.sum_xy
.Solution:
x = 5
y = 10
sum_xy = x + y
print("Sum of x and y:", sum_xy)
pi
.greeting
.is_active
.pi
, greeting
, and is_active
.Solution:
pi = 3.14
greeting = "Hello, World!"
is_active = True
print("Type of pi:", type(pi), "Value:", pi)
print("Type of greeting:", type(greeting), "Value:", greeting)
print("Type of is_active:", type(is_active), "Value:", is_active)
first_name
.last_name
.first_name
and last_name
with a space in between and assign the result to a variable named full_name
.full_name
.Solution:
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print("Full name:", full_name)
True
to a variable named is_sunny
.False
to a variable named is_raining
.can_go_outside
that is True
if is_sunny
is True
and is_raining
is False
.can_go_outside
.Solution:
is_sunny = True
is_raining = False
can_go_outside = is_sunny and not is_raining
print("Can go outside:", can_go_outside)
"123"
to a variable named num_str
.num_str
to an integer and assign it to a variable named num_int
.num_int
.Solution:
num_str = "123"
num_int = int(num_str)
print("Type of num_int:", type(num_int), "Value:", num_int)
Basic Data Types
int
), floating-point numbers (float
), and complex numbers (complex
).str
) to represent sequences of characters.bool
) for True or False.Boolean and None
None
None
is a singleton in Python, meaning there is only one instance of None
in a Python runtime. All occurrences of None
point to the same object.
a = None
b = None
print(a is b) # Output: True
None
is NoneType
.
print(type(None)) # Output: <class 'NoneType'>
None
is treated as False
in a boolean context.
if not None:
print("None is considered False") # Output: None is considered False
None
None
, use the is
operator, as it checks for identity.
variable = None
if variable is None:
print("Variable is None") # Output: Variable is None
How do you check if a variable is None?
variable = None
if variable is None:
print("Variable is None")
Type Casting
[1]R. Python, “Dynamic vs Static – Real Python,” realpython.com. https://realpython.com/lessons/dynamic-vs-static/ [2]Python Software Foundation, “Built-in Types — Python 3.12.1 documentation,” Python.org, 2019. https://docs.python.org/3/library/stdtypes.html [3]“PEP 526 – Syntax for Variable Annotations | peps.python.org,” peps.python.org. https://peps.python.org/pep-0526/