File: Unsaved Document 1 Page 1 of 10
String:
String is sequence of characters which could be alphabet,number,special character and space.
Creating
* Using Single and double quote
a = 'hlo'
a = "hlo"
* Using triple quote for multiline
a = ''' Hlo
world '''
Acessing element(or item) in string
String item could accessed using Indexing []
a[0]
slicing/Range
Its method of accessing mutiple item instead of one
[0:6] items from 0 to 6 (1,2,3,4,5) but not 6.
Way of slicing
[0:4]
[:4]
[2:]
[-3:-1]
----------------------------------------------------------------------------------------
List
List is collection of items
Created
It is created uing [] sybol
For example: a = [1,3,4]
Acessing
Item in list could be access using index
print(a[0])
Chaning value
Value in list can be changed using index []
Ex: a[0] = 6
Adding new value/Appending
append() is used to insert new item at end of list
insert() is used to insert new item anywhere in list using Index No
Deleting
Items in list can be deleted using:
del word
Example: del a[0]
pop()
It remove item from end of list
remove()
It removes a value from list
------------------------------------------------------------------------------------
Dictionary
It is collection of items in key value pair
File: Unsaved Document 1 Page 2 of 10
Creation
Its created using {} symbol
Example: a = {'a':1,'b':2}
Access
Item in dictionary could be accessed using key
a['a']
Changing value
a['a] = 5
Adding new value
a['c'] = 3
Deleting value
Using del word
del a['c']
Using pop()
a.pop() It removes last item from dictionary
Using clear
a.clear()
It removes everything from dictionary
-------------------------------------------------------------------------------------------
Tuple:
It is sequence of items that are immutable and cant be changed once declared.
Creation
Using () symbol
a = (1,2,3,4)
Tuple not have any way to add,delete or change value so its immutable as python not provide any way to do.
------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
All functions and methods
-----------------------------
String
len(string):
Returns the length of the string.
string.capitalize():
Convert first character to Uppercase and other all lower case
File: Unsaved Document 1 Page 3 of 10
string.istitle():
Returns True if first character in string is Uppercase
string.lower():
Returns lowercased string
string.islower():
check if all characters in the string are lowercase
string.isupper():
Returns True if all characters in the string are uppercase
string.upper():
Returns uppercased string
string.swapcase():
convert the case of string to opposite
string.find(substring):
Returns the lowest index Number of the substring if found
string.isalnum():
checks if all characters in the string are alphanumeric (letters or numbers)
string.isalpha():
checks if all characters in the string are alphabetic (letters)
string.isdigit():
checks if all characters in the string are digits
string.lstrip():
Remove the whitespace in begining of string and return it
File: Unsaved Document 1 Page 4 of 10
string.rstrip():
Remove the whitespace in end of string and return it
string.isspace():
Returns True if all characters in the string are whitespace
string.partition():
Splits the string at the first occurrence of sep and returns a tuple containing the part before sep, the sep
itself, and the part after sep.
string.replace(old, new):
replace old with new
string.count():
Count the number of time chracter or substring is repeated
---------------------------------------------------------
List:
len(list):
Returns the number of items in the list.
list.insert(index, element):
Inserts the element at the specified index in the list.
list.append(element):
Appends the element to the end of the list.
list.extend(iterable):
Appends elements from the iterable to the end of the list.
File: Unsaved Document 1 Page 5 of 10
list.sort():
By defaultsort list in asending order
list.remove(element):
Removes the first occurrence of element from the list.
list.reverse():
Reverses the order of the elements in the list in place.
list.pop():
Removes and returns the element at the specified index from the list. If index is not specified, removes and
returns the last element.
list.count(element):
Returns the number of occurrences of element in the list.
list.index():
Returns the lowest index Number of item in the list
max(list):
Returns the maximum value in the list.
min(list):
Returns the minimum value in the list.
------------------------------------------------------------------------------------
Dictionary:
len(dict):
Returns the number of key-value pairs in the dictionary.
dict.pop():
Removes the item with the specified key from the dictionary and returns its value. If the key is not found, it
returns the default value (which defaults to raising a KeyError if not provided).
File: Unsaved Document 1 Page 6 of 10
dict.clear():
Removes all items from the dictionary.
dict.update():
Updates the dictionary with key-value pairs from another dictionary or an iterable of key-value pairs.
dict.get(key):
Returns the value associated with the specified key.
dict.items():
Returns key-value pairs in the dictionary.
dict.keys():
Returns all the keys in the dictionary.
dict.values():
Returns all the values in the dictionary.
dict():
Creates a new dictionary or convert other into dictionary
---------------------------------------------------------------------------------------
Tuple:
len(tuple):
Returns the number of elements in the tuple.
max(tuple):
Returns the maximum value in the tuple.
min(tuple):
File: Unsaved Document 1 Page 7 of 10
Returns the minimum value in the tuple.
tuple():
Converts an list into a tuple.
tuple.index():
Returns the index Number of the first occurrence of value
tuple.count(value):
Returns the number of occurrences of value in the tuple.
sum():
Returns the sum of all elements
any(iterable):
Returns True if at least one element in the iterable is true, otherwise False.
all(iterable):
Returns True if all elements in the iterable are true, otherwise False.
sorted(iterable, key=None, reverse=False):
Returns a new sorted tuple
reversed(seq):
Returns reversed tuple
-----------------------------------------------------------------------------------
Operators:
In Python, operators are special symbols that perform operations on operands
Arithmetic Operators:
Addition +
Subtraction -
File: Unsaved Document 1 Page 8 of 10
Multiplication *
Division /
Floor Division //
Modulus %
Exponentiation **
Comparison Operators:
Equal to ==
Not equal to !=
Greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=
Logical Operators:
Logical AND and
Logical OR or
Logical NOT not
Bitwise Operators:
Bitwise AND &
Bitwise OR |
Bitwise XOR ^
Bitwise NOT ~
Left shift <<
Right shift >>
Assignment Operators:
Assignment =
Addition Assignment +=
Subtraction Assignment -=
Multiplication Assignment *=
Division Assignment /=
Modulus Assignment %=
Membership Operators:
in: Give True if the specified value is found in the sequence (e.g., lists, tuples, strings, etc.).
not in: Give True if the specified value is not found in the sequence.
Identity Operators:
is: Give True if two object have same memory location
is not: Gives True object not have same memory location.
-------------------------------------------------------------------------------------------
Conditional Structures:
If Statement:
Explanation: Executes a block of code if a specified condition is true.
Syntax:
File: Unsaved Document 1 Page 9 of 10
if condition:
# block of code
If...Else Statement:
Explanation: Executes one block of code if the condition is true and another block of code if the condition is
false.
Syntax:
if condition:
# block of code
else:
# block of code
If...Elif...Else Statement:
Explanation: Executes different blocks of code depending on which condition is true.
Syntax:
if condition1:
# block of code
elif condition2:
# block of code
else:
# block of code
------------------------------------------------------------------------------------------
Loop:
Loop execute piece of code repeatly
For Loop:
Explanation: Executes a block of code repeatedly for each item in an iterable.
Syntax:
for variable in iterable:
# block of code
While Loop:
Explanation: Executes a block of code repeatedly as long as a specified condition is true.
Syntax:
while condition:
# block of code
-------------------------------------------------------------------------
List composite
Means list containing different object in one list or nested list.
a = [[1,2],[3,4]]
File: Unsaved Document 1 Page 10 of 10