Data Types In Python
Numbers : |
> Python supports three types of numbers: integers, floating-point numbers, and complex numbers. > Integers are whole numbers without a decimal point, floating-point numbers have a decimal point, and complex numbers have both real and imaginary components. x = 5 # integer y = 3.14 # floating-point number z = 2 + 3j # complex number |
Lists In Python
Lists |
Lists are ordered sequences of values that can be of any data type. They are mutable, which means that you can add, remove, or modify elements in a list. fruits = ["apple", "banana", "cherry"] print(fruits) # output: ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) # output: ["apple", "banana", "cherry", "orange"] fruits.remove("banana") print(fruits) # output: ["apple", "cherry", "orange"] |
Booleans In Python
Booleans |
Boolean values represent either True or False. They are used for logical operations and control flow statements, such as if-else statements and loops. is_raining = True is_sunny = False print("Bring an umbrella") else: print("Enjoy the sunshine") |
|
|
Strings in Python
Strings |
Strings are sequences of characters that are enclosed in single or double quotes. They can be manipulated in various ways, such as concatenation, slicing, and formatting message = "Hello, World!" print(message) # output: Hello, World! print(message[0]) # output: H print(message[7:12]) # output: World formatted_message = "My name is {} and I am {} years old".format("John", 25) print(formatted_message) # output: My name is John and I am 25 years old |
Tuples In Python
Tuples |
Tuples are similar to lists in that they are ordered sequences of values, but they are immutable, which means that you cannot modify them after they are created coordinates = (10, 20) print(coordinates) # output: (10, 20) x, y = coordinates print(x) # output: 10 |
Dictionaries in Python
Dictionaries |
Dictionaries are unordered collections of key-value pairs, where each key is unique. They are commonly used for data modeling and organizing data. person = {"name": "John", "age": 25, "address": "123 Main St"} print(person) # output: {"name": "John", "age": 25, "address": "123 Main St"} print(person["name"]) # output: John person["phone"] = "555-1234" print(person) # output: {"name": "John", "age": 25, "address": "123 Main St", "phone": "555-1234"} |
|
|
|