DataType in Python
The type() function can be used to define the values of various data types and to check their data types.
# DataType Output: str
x = "Hello World"
# DataType Output: int
x = 50
# DataType Output: float
x = 60.5
# DataType Output: complex
x = 3j
# DataType Output: list
x = ["geeks", "for", "geeks"]
# DataType Output: tuple
x = ("geeks", "for", "geeks")
# DataType Output: range
x = range(10)
# DataType Output: dict
x = {"name": "Suraj", "age": 24}
# DataType Output: set
x = {"geeks", "for", "geeks"}
# DataType Output: frozenset
x = frozenset({"geeks", "for", "geeks"})
# DataType Output: bool
x = True
# DataType Output: bytes
x = b"Geeks"
# DataType Output: bytearray
x = bytearray(4)
# DataType Output: memoryview
x = memoryview(bytes(6))
# DataType Output: NoneType
x = None
List in Python
The Python list is a sequence data type that is used to store the collection of data. Tuples and String are other types of sequence data types.
Var = ["Geeks", "for", "Geeks"]
print(Var)
Output:
['Geeks', 'for', 'Geeks']
List comprehension
A Python list comprehension is made up of brackets carrying the expression, which is run for each element, as well as the for loop, which is used to iterate over the Python list’s elements.
Also, Read – Python Array
# Using list comprehension to iterate through loop
List = [character for character in [1, 2, 3]]
# Displaying list
print(List)
Output:
[1, 2, 3]
Dictionary in Python
A dictionary in Python is a collection of key values, used to store data values like a map, which, unlike other data types holds only a single value as an element.
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(Dict)
Output:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Python Dictionary Comprehension
Like List Comprehension, Python allows dictionary comprehension. We can create dictionaries using simple expressions. A dictionary comprehension takes the form {key: value for (key, value) in iterable}
# Lists to represent keys and values
keys = ['a','b','c','d','e']
values = [1,2,3,4,5]
# but this line shows dict comprehension here
myDict = { k:v for (k,v) in zip(keys, values)}
# We can use below too
# myDict = dict(zip(keys, values))
print (myDict)
Output:
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Tuples in Python
Tuple is a list-like collection of Python objects. A tuple stores a succession of values of any kind, which are indexed by integers.
var = ("Geeks", "for", "Geeks")
print(var)
Output:
('Geeks', 'for', 'Geeks')
Sets in Python
Python Set is an unordered collection of data types that can be iterated, mutated and contains no duplicate elements. The order of the elements in a set is unknown, yet it may contain several elements.
var = {"Geeks", "for", "Geeks"}
print(var)
Output:
{'for', 'Geeks'}
Python Cheat sheet (2024)
Python is one of the most widely-used and popular programming languages, was developed by Guido van Rossum and released first in 1991. Python is a free and open-source language with a very simple and clean syntax which makes it easy for developers to learn Python. It supports object-oriented programming and is most commonly used to perform general-purpose programming. Python is used in several domains like Data Science, Machine Learning, Deep Learning, Artificial Intelligence, Scientific Computing Scripting, Networking, Game Development Web Development, Web Scraping, and various other domains.
To give a comprehensive overview of Python programming, we made a Python Cheat Sheet for Python programmers. In this Cheat Sheet of Python, you’ll learn all the basic to advanced topics and concepts of Python, like Python data types, Python for loop, Python slice, python map function, python dictionary, Python File Handling, etc.
Python Cheat sheet
- What is Python?
- Python Input
- Operators in Python
- Python Comment
- DataType in Python
- Python String
- Conditional Statements
- Python Functions
- Python BuildIn Function
- Python RegEx
- File Handling in Python
- Try and Except Statement
- Python OOPs Concepts
- Python Modules
- Python Interview Questions Answers
- Python Cheat Sheet – FAQs