Variables and Data Types of Python by Meta Ai | Ballu Python 835430
Here's the information on variables and data types in Python, along with practical examples:
Variables in Python
In Python, variables are used to store and manipulate data. To assign a value to a variable, use the assignment operator (=).
Example:
x = 5 # x is an integer variable
y = "Hello" # y is a string variable
print(x) # Output: 5
print(y) # Output: Hello
Data Types in Python
Python has several built-in data types:
1. Integers (Int)
Integers are whole numbers.
Example:
x = 5
y = -10
print(x) # Output: 5
print(y) # Output: -10
2. Floats (Float)
Floats are decimal numbers.
Example:
x = 3.14
y = -0.5
print(x) # Output: 3.14
print(y) # Output: -0.5
3. Strings (Str)
Strings are sequences of characters.
Example:
x = "Hello"
y = 'World'
print(x) # Output: Hello
print(y) # Output: World
4. Boolean (Bool)
Booleans represent true or false values.
Example:
x = True
y = False
print(x) # Output: True
print(y) # Output: False
5. Lists (List)
Lists are ordered collections of items.
Example:
x = [1, 2, 3, "Hello", True]
print(x) # Output: [1, 2, 3, 'Hello', True]
print(x[0]) # Output: 1
print(x[4]) # Output: True
6. Tuples (Tuple)
Tuples are ordered, immutable collections of items.
Example:
x = (1, 2, 3, "Hello", True)
print(x) # Output: (1, 2, 3, 'Hello', True)
print(x[0]) # Output: 1
print(x[4]) # Output: True
7. Dictionaries (Dict)
Dictionaries are unordered collections of key-value pairs.
Example:
x = {"name": "John", "age": 30, "city": "New York"}
print(x) # Output: {'name': 'John', 'age': 30, 'city': 'New York'}
print(x["name"]) # Output: John
print(x["age"]) # Output: 30
8. Sets (Set)
Sets are unordered, mutable collections of unique items.
Example:
x = {1, 2, 3, "Hello", True}
print(x) # Output: {1, 2, 3, 'Hello', True}
Comments
Post a Comment