Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰
Python2. Data Types

πŸ”  Data Types, Type Casting and Inputs

πŸ“Š Data Types

Python is a dynamically typed language, which means you don’t need to explicitly declare a variable’s type. The type is inferred at runtime.

data_types.py
# String name = "John Doe" # Textual data # Integer age = 30 # Whole number # Float height = 5.9 # Decimal number # Boolean is_student = True # True or False value # List fruits = ["apple", "banana", "cherry"] # Mutable ordered collection of items # Tuple fruits_tuple = ("apple", "banana", "cherry") # Immutable ordered collection # Set fruits_set = {"apple", "banana", "cherry"} # Unordered collection of unique items # Dictionary person = { "name": "John Doe", "age": 30, "height": 5.9, "is_student": True } # Key-value pairs

➑️ Type Casting

Process of converting a variable from one data type to another. Methods include int(), float(), str(), and bool().


Type checking can be done using type() function.

type_casting.py
name = "John Doe" age = 30 height = 5.9 is_student = True # Type checking print(type(name)) # Output: <class 'str'> print(type(age)) # Output: <class 'int'> print(type(height)) # Output: <class 'float'> print(type(is_student)) # Output: <class 'bool'> # Type casting height = int(height) is_student = str(is_student) print(height) # Output: 5 print(is_student) # Output: True

πŸ”  How to take input from user?

You can take input from the user using the input() function. The input is always returned as a string, so you may need to convert it to the appropriate data type using type casting.

input.py
name = input("Enter your name: ") age = int(input("Enter your age: ")) height = float(input("Enter your height: ")) is_student = input("Are you a student? (True/False): ") == "True" print(f"Name: {name}") print(f"Age: {age}") print(f"Height: {height}") print(f"Is Student: {is_student}")
Last updated on