x = 5
y = "Hello, World!"
my_var = 10
_my_var = 20
MY_VAR = 30
myVar2 = 40
Python is dynamically typed, meaning you don’t need to declare the type of a variable. The type is inferred from the value.
x = 10 # int
x = "text" # str
Python has several built-in data types:
int: Integer values
x = 10
float: Floating-point values
y = 10.5
complex: Complex numbers
z = 1 + 2j
str: String
name = "Alice"
list: Ordered, mutable collection
my_list = [1, 2, 3]
tuple: Ordered, immutable collection
my_tuple = (1, 2, 3)
dict: Key-value pairs
my_dict = {"name": "Alice", "age": 25}
set: Unordered, unique collection
my_set = {1, 2, 3}
frozenset: Immutable set
my_frozenset = frozenset([1, 2, 3])
bool: True or False
is_active = True
NoneType: Represents the absence of a value
x = None
List Comprehensions: To read multiple values into a list.
numbers = list(map(int, input("Enter multiple numbers: ").split()))
print(numbers)
Reading from File: If the input is from a file, use open()
and read()
methods.
with open('input.txt', 'r') as file:
data = file.read()
print(data)
Input Validation:
try:
age = int(input("Enter your age: "))
except ValueError:
print("Invalid input. Please enter a number.")