Data types in Python with examples PDF
Data types in Python with examples PDF
I can't provide a PDF file for you, but I can give you an overview of data types in Python along with some examples:
Python is a dynamically-typed language, which means that the type of a variable is determined at runtime rather than compile time. Python has several built-in data types, and they are used to store values of different types.
Integers (int): These are whole numbers, either positive or negative. Example: x = 5 Floats (float): These are decimal numbers. Example: y = 3.14 Strings (str): These can be a sequence of characters, including letters, digits, and special characters. Example: name = "John" Boolean (bool): These represent True or False values. Example: isAdmin = True List (list): These are ordered collections of objects. Example: students = [1, 2, 3] Tuple (tuple): These are immutable ordered collections of objects. Example: coordinates = (1, 2) Dictionary (dict): These are unordered collections of key-value pairs. Example: student_data = {"name": "John", "age": 25} Set (set): These are unordered collections of unique elements. Example: numbers = {1, 2, 3, 4, 5}Here is a more detailed explanation:
Integers: Python integers can be either positive or negative whole numbers.
x = 5
y = -10
print(x) # prints 5
print(y) # prints -10
Floats: Python floats are decimal numbers. They can have fractional parts.
x = 3.14
y = -0.1
print(x) # prints 3.14
print(y) # prints -0.1
Strings: Strings in Python are sequences of characters, including letters, digits, and special characters. They can be enclosed in single quotes (') or double quotes (").
name = "John"
greeting = 'Hello'
print(name) # prints John
print(greeting) # prints Hello
Boolean: Boolean values in Python represent either True or False.
isAdmin = True
isUser = False
print(isAdmin) # prints True
print(isUser) # prints False
List: Lists are ordered collections of objects. They can be indexed and sliced like strings.
students = [1, 2, 3]
print(students[0]) # prints 1
print(students[-1]) # prints 3
Tuple: Tuples are immutable ordered collections of objects. Like lists, they can be indexed and sliced like strings.
coordinates = (1, 2)
print(coordinates[0]) # prints 1
print(coordinates[-1]) # prints 2
Dictionary: Dictionaries are unordered collections of key-value pairs. They use keys to access values.
student_data = {"name": "John", "age": 25}
print(student_data["name"]) # prints John
print(student_data.get("age")) # prints 25
Set: Sets are unordered collections of unique elements. They can be used to quickly find the union, intersection, or difference of sets.
numbers = {1, 2, 3, 4, 5}
print(numbers) # prints {1, 2, 3, 4, 5}
What is list data type in python with example w3schools
I'm happy to help!
In Python, a list
is a collection of items that can be of any data type, including strings, integers, floats, and other lists. Lists are denoted by square brackets []
. Here's an example from W3Schools:
# Create a list
fruits = ['apple', 'banana', 'cherry']
print(fruits)
When you print the list, Python displays the elements in the order they appear within the list. You can also access specific elements using their index (position). Indexing starts at 0, so the first element is at index 0.
For instance:
# Access the second fruit
print(fruits[1])
This will output banana
.
Here are some key features of Python lists:
Indexing: Lists are zero-indexed, meaning that the first element has an index of 0. The last element is indexed by one less than the length of the list. Slicing: You can extract a subset of elements from the list using slicing notation:my_list[start:stop]
. For example:
print(fruits[1:]) # Output: ['banana', 'cherry']
print(fruits[:-1]) # Output: ['apple', 'banana']
Appending: You can add new elements to the end of a list using the append()
method. For instance:
fruits.append('orange')
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
Inserting: You can insert elements at specific positions within the list using the insert()
method.
fruits.insert(1, 'mango') # Insert 'mango' at index 1
print(fruits) # Output: ['apple', 'mango', 'banana', 'cherry']
Removing: You can remove elements from a list using the remove()
method or the pop()
method.
fruits.remove('banana') # Remove the first occurrence of 'banana'
print(fruits) # Output: ['apple', 'mango', 'cherry']
fruits.pop(1) # Remove the second element (index 1)
print(fruits) # Output: ['apple', 'cherry']
Sorting: You can sort elements within a list using the sort()
method. For instance:
numbers = [5, 2, 8, 3]
numbers.sort()
print(numbers) # Output: [2, 3, 5, 8]
W3Schools has an excellent tutorial on Python lists, which covers these topics and more: https://www.w3schools.com/python/ref_list.asp
If you have any questions or need further clarification, feel free to ask!