Python Code
Python Code
Python Code
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 '''
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
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
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.
------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
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():
string.lower():
string.islower():
string.isupper():
string.upper():
string.swapcase():
string.find(substring):
Returns the lowest index Number of the substring if found
string.isalnum():
string.isalpha():
string.isdigit():
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():
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():
---------------------------------------------------------
List:
len(list):
Returns the number of items in the list.
list.insert(index, element):
list.append(element):
list.extend(iterable):
list.sort():
By defaultsort list in asending order
list.remove(element):
list.reverse():
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):
list.index():
max(list):
min(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():
dict.update():
Updates the dictionary with key-value pairs from another dictionary or an iterable of key-value pairs.
dict.get(key):
dict.items():
dict.keys():
dict.values():
dict():
---------------------------------------------------------------------------------------
Tuple:
len(tuple):
Returns the number of elements in the tuple.
max(tuple):
min(tuple):
File: Unsaved Document 1 Page 7 of 10
tuple():
tuple.index():
tuple.count(value):
sum():
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.
reversed(seq):
-----------------------------------------------------------------------------------
Operators:
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:
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:
if condition1:
# block of code
elif condition2:
# block of code
else:
# block of code
------------------------------------------------------------------------------------------
Loop:
Loop execute piece of code repeatly
For Loop:
While Loop:
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