PYTHON
*Data Types:
1. STRING
Subscript: print("HELLO"[0]] - [0] is index
- output will be H
Concatenation: print(“123” + “456”)
- Output will be 12345
- Can concat only string to string but not string to int
2. INTEGER
Arithmetic Operation: print(123 + 456)
- Output will be their sum = 579
Large Numbers: ( 123_456_789)
- ‘_’ represent the commas in the number system
3. FLOAT
Floating Point Number:
- 3.14159 (float) and 3141.59 (floating point number)
- Print(70+ float(100)) or print(70 + float(“100”))
- OUTPUT: 170.0
4. BOOLEAN: (True or )
* type() FUNCT :
- Tells which data type is the given input
- num=3
print(type(num))
- output = <class ‘int’>
* Str() FUNCT :
- Converts an integer into string
- makes it easier to use concatenation (only for strings)
- EXAMPLE 1: num=len(input(“enter ur name:”)
new_num=str(num)
Print(“ your name has ” + new_num + “ letter”)
OUTPUT : your name has 10 letters
- EXAMPLE 2: num=str(123)
Print(type(num))
OUTPUT: <class ‘str’> (conversion of int to str)
-EXAMPLE 3: print(str(70) + str(100))
OUTPUT: 70100 (CONCATENATION)
PEMDASLR : (Order)
Parenthesis
Exponents
Multiplication
Division
Addition
Subtraction
Left
Right
- EXAMPLE: print(3*3+3/3-3)
OUTPUT: 7.0
Round() FUNCT:
- gives the round off values of float values
- EXAMPLE: print(round(1.567))
OUTPUT: 2
-EXAMPLE: print(round(1.567,2))
OUTPUT: 1.57
Floor div:
- EXAMPLE: print(8//3)
- OUTPUT: 2
* CONDITIONAL OPERATORS AND if/else :
- if condition:
Do this
Else:
Do this
- > Greater than
< Less than
>= Greater than or equal to
<= Less than or equal
== Equal to (Assignment)
!= Not equal to
* NESTED IF / ELSE :
if condition:
if another condition:
do this
else:
do this
else:
do this
if / elif / else
if condition1:
do A
elif condition2:
do B
else:
do this
Multiple if :
If cond1:
Do A
If cond2:
Do B
If cond3:
Do C
* LOGICAL OPERATORS:
A and B
C or D
Not E
* Random module:
- Generates random numbers
-Syntax:
Import random
1. seed() :
- initialize the random number generator.
- Syntax:
random.seed (a,version)
a ( optional )
version ( An integer specifying how to convert the a parameter into a
integer. Default value is 2 )
- Example:
import random
random.seed(10)
print(random.random())
OUTPUT: 0.5714025946899135
2. randrange() :
- returns a randomly selected element from the specified range.
- Syntax:
random.randrange ( start, stop , step )
start (Default 0)
stop (position to end)
step ( incrementation - Default 1 )
- EXAMPLE :
import random
print(random.randrange(3, 9)) 3 is included and 9 is not included
OUTPUT :
5 ( generates random number )
3. randint():
- returns an integer number selected element from the specified range
- Syntax :
random.randint(start, stop)
- EXAMPLE:
import random
print(random.randint(3, 9)) both are included
OUTPUT:
4. CHOICE():
- returns a randomly selected element from the specified sequence.
- The sequence can be a string, a range, a list, a tuple or any other kind of
sequence.
- Syntax :
random.choice(sequence)
- EXAMPLE1:
import random
mylist = ["apple", "banana", "cherry"]
print(random.choice(mylist))
OUTPUT:
Apple
- EXAMPLE2:
import random
x = "WELCOME"
print(random.choice(x))
OUTPUT :
5. random() :
- generates random floating numbers between 0 to 1 .
- Syntax:
random.random()
- Example:
import random
h= random.random()
OUTPUT: 0.06574383
6. uniform():
- returns a random floating number between the two specified numbers
(both included).
- Syntax :
random.uniform(a, b)
- Example :
import random
print(random.uniform(20, 60))
OUTPUT:
52.343343563
* LISTS :
- Syntax:
List= [ item1 , item2 ]
- EXAMPLE 1:
List= list(("cherry","mango"))
print(List)
OUTPUT: ['cherry', 'mango']
- EXAMPLE 2:
List=[ “cherry”,”mango”,”banana” ]
Print(List[0])
OUTPUT : cherry
Access list items:
a) Range of indexes:
thislist=["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[2:5])
#This will return the items from position 2 to 5.
#Remember that the first item is position 0,
#and note that the item in position 5 is NOT included
OUTPUT: ['cherry', 'orange', 'kiwi']
b) Check if items exists:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
OUTPUT: Yes, 'apple' is in the fruits list
c) Change item value :
list=["cherry","mango"]
list[1]="strawberry"
print(list)
OUTPUT: ['cherry', 'strawberry']
-> Change item value:
d) Change a range of item value:
list=[“cherry”,”mango”,”strawberry”,”apple”,”banana”]
list[1:3]=[“blueberry”,”orange”]
print(list)
OUTPUT: [‘cherry’,’blueberry’,’orange’,’apple’,’banana’]
e) Insert items: insert() – insert a new list item , without
replacing existing values
list = [“apple”,”banana”,”cherry”]
list.insert(2,”mango”)
print(list)
OUTPUT: [‘apple’,’banana’,mango’,’cherry’]
-> Add list items:
f) Append items: To add an item to the end of the list
list=['apple','banana','orange','mango']
list.append("watermelon")
print(list)
OUTPUT: ['apple', 'banana', 'orange', 'mango', 'watermelon']
g) Extend List: To append elements from another list to the current
list
list=['apple','banana','orange']
l=['cherry','mango']
list.extend(l)
print(list)
OUTPUT: ['apple', 'banana', 'orange', 'cherry', 'mango']
h) Add Any Iterable: The extend() method does not have to
append lists, you can add any iterable object (tuples, sets,
dictionaries etc.).
list=['apple','banana','orange']
tuple=('cherry','mango')
list.extend(tuple)
print(list)
OUTPUT: ['apple', 'banana', 'orange', 'cherry', 'mango']
-> Remove list items:
i) Remove a specified item: remove() method removes the specified
item
list=['apple','banana','orange']
list.remove('apple')
print(list)
OUTPUT: ['banana', 'orange']
j) Remove specified index: pop() method removes the specified index.
list=['apple','banana','orange']
list.pop(1)
print(list)
OUTPUT: ['apple', 'orange']
- If index is not specified , last item
will be removed.
list=['apple','banana','orange']
list.pop()
print(list)
OUTPUT: ['apple', 'banana']
-The del keyword also removes the specified
index
list=['apple','banana','orange']
del list[2]
print(list)
OUTPUT: ['apple', 'banana']
- Deleting the entire list
list=['apple','banana','orange']
del list
print(list)
k) clear the list: The clear() method empties the list.
list1=['apple','banana','orange']
list1.clear()
print(list1)
OUTPUT: []