timeit
The timeit module in Python provides a simple way to measure the execution time of small bits of Python code. It offers both a command-line interface and a callable one.

from timeit import default_timer as timer: This line imports thedefault_timerfunction from thetimeitmodule and renames it totimer.start = timer(): This line gets the current time using thetimerfunction and stores it in thestartvariable.a = 'a' * 6: This line creates a string variableathat contains six 'a' characters.stop = timer(): This line gets the current time again using thetimerfunction and stores it in thestopvariable.print(stop - start): This line calculates the difference between the start and stop times (i.e., the time it took to execute the code between thestartandstoplines) and prints it to the console.
The output of the code will be the time it took to execute the code in seconds.
LIST
→ use .copy() to make a copy of a list otherwise it will tamper the original list

TUPLES
Tiples:- ordered, immutable, allows duplicate elements
Tulpe is more efficient than a list
Notice the “ , ” :

To create a tuple with a single value, you need to add a comma after the value.

Result:
1
[2, 3, 4, 5, 6, 7]
8

Result:
[1, 2, 3, 4, 5, 6]
7
8
The expression *i1, i2, i3 = a is a multiple assignment statement that assigns the first six elements of a to the variable i1 as a list (since the * operator is used to unpacking the first six elements), the seventh element of a to the variable i2, and the eighth element of a to the variable i3.
Dictionary
Key-value pairs, Unordered, Mutable
Dictionary created using “ dict() ” function:

Result:
27
{‘name’: ‘Brundon’, ‘age’: 27, ‘city’: ‘boston’}
{‘name’: ‘Brundon’, ‘age’: 27, ‘city’: ‘boston’, ‘hobby’: ‘basketball’}
{‘name’: ‘Brundon’, ‘age’: 27, ‘city’: ‘boston’}
Error

Result:
name
age
city
— — — — — — — — — — — -
Brundon
27
boston
— — — — — — — — — — — -
name Brundon
age 27
city boston

Result:
{‘name’: ‘Rahul’, ‘age’: ‘22’, ‘city’: ‘NewYork’}

Result:
{‘name’: ‘Brundon’, ‘age’: 27, ‘city’: ‘boston’}
{(1, 2): 45}
SETS
Sets > unordered, mutable, no duplicates
A frozen set in Python is an immutable (unchangeable) set. Like a regular set, it is an unordered collection of unique elements, but once created, its elements cannot be modified.
Cannot add or remove elements

>>> frozenset({1, 2, 3})
STRING
Stings are immutable, ordered, text representation
strip():
The “strip()” function will get rid of the white space

startswith()
The startswith()checks if a string starts with a specified substring. It returns a boolean value (True or False) based on whether the string starts with the given substring.

split() and join()

format

Comments
Post a Comment