Data Types and Variables in Python
Python is a dynamically-typed language, which means that variables do not need to be declared before they are used. Variables can be assigned values of any data type, such as numbers, strings, and lists.
Python Data Types
Python has several built-in data types, including:
- Numbers: integers, floating-point numbers, and complex numbers
- Strings: sequences of characters
- Lists: ordered sequences of values
- Tuples: ordered, immutable sequences of values
- Dictionaries: unordered collections of key-value pairs
- Sets: unordered collections of unique values
- Booleans: logical values True and False
- NoneType: a special type that represents the absence of a value
Each data type has its own set of operations and methods. For example, you can concatenate two strings using the +
operator:
first_name = 'John'
last_name = 'Doe'
full_name = first_name + ' ' + last_name
print(full_name) # Output: 'John Doe'
You can also perform arithmetic operations on numbers:
x = 3
y = 2
print(x + y) # Output: 5
print(x * y) # Output: 6
print(x / y) # Output: 1.5
Python Variables
In Python, variables are used to store values of different data types. Variables are created when you assign a value to them. For example:
x = 42
y = 'Hello, world!'
z = [1, 2, 3]
You can also assign multiple variables at once using a technique called unpacking:
x, y, z = 1, 2, 3
print(x, y, z) # Output: 1 2 3
Conclusion
Python's dynamic typing and built-in data types make it a versatile language for working with different types of data. Variables can be assigned values of any data type, and each data type has its own set of operations and methods.