Beginner âĸ Python
Data Types
đ§Š What Are Data Types?
A data type tells Python what kind of value we are working with. For example, a number and a piece of text are different types of data. Understanding data types helps us know what we can do with a value.
age = 12
height = 4.8
name = "Alex"
is_student = True
print(age)
print(height)
print(name)
print(is_student)12 4.8 Alex True
đ Everything in Data Types
Learn about the different types of data Python can store and work with. Here is every step in this topic in one place, so you can review what you learned or jump straight to a step.
đ§Š What Are Data Types?
A data type tells Python what kind of value we are working with. For example, a number and a piece of text are different types of data. Understanding data types helps us know what we can do with a value.
age = 12 height = 4.8 name = "Alex" is_student = True print(age) print(height) print(name) print(is_student)đĸ Numbers: int and float
Python has two basic number types that you will use often: int and float. An int is a whole number without a decimal point. A float is a number that contains a decimal point.
age = 12 score = 100 apples = 5 height = 4.8 price = 12.50 print(age) print(height)đ¤ Strings: Working with Text
A string is text stored inside quotation marks. You can use either single quotes or double quotes. Strings can contain letters, numbers, spaces, and symbols.
name = "Alex" city = 'London' message = "Hello!" print(name) print(city) print(message)â Booleans: True and False
A boolean is a value that can be either True or False. Booleans are useful when a program needs to make decisions, such as checking whether a player has finished a game or whether it is raining.
is_sunny = True is_raining = False print(is_sunny) print(is_raining)đ Checking Data Types with type()
Python provides a useful function called type() that tells us what type of data a value contains. This is helpful when learning Python and when checking values inside a program.
age = 12 name = "Alex" height = 4.8 is_student = True print(type(age)) print(type(name)) print(type(height)) print(type(is_student))đ Changing Data Types
Sometimes we need to convert one type of data into another. This is called type conversion. Common conversion functions include int(), float(), and str().
age = "12" age = int(age) price = 5 price = float(price) score = 100 score_text = str(score) print(age) print(price) print(score_text)
