Uploaded by Amodh YH

Basic Python programing short Note

advertisement
Python
1)Functions
1. Print() – output
2. Input()
3. Len()-count
4. range(2)- 0,1
5. round(2.9)=3
6. abs(-2.9) - +2.9
2)Methods
1. Upper() – Capitalization
2. Lower()- de-capitalization
3. Printing specific character/s in a string
Ex:String=‘Python for beginners’
Print(string[0])
The output would be – P
If we want to print a single wordString=’Python for beginners’
Print(string[0:6])
Output would be- Python
#)You can also number from the end of the string
ExString=’That Dog is black’
Print(String[-1:-6])
The output would be – black
The table below shows how the strings number itself.
Matrixes in Python
You can simply make a matrix by the concept list of the list
Ex:matrix
=[
[2,5,8],
[5,9,1],
[7,9,6]
]
T
-17
h
-16
a
-15
t
-14
-13
d
-12
o
-11
g
-10
-9
i
-8
s
-7
-6
b
-5
l
-4
a
-3
c
-2
k
-1
*Notice that at the end of the last row of the matrix there is no comma(,)
If we want to display the full parts of the matrix we should use a nested loop twice.
Ex:For I in matrix:
Print(i)
The result would be
[2,5,8]
[5,9,1]
[7,9,6]
Each list considered as an one item so it printed 3 items
If we want to access the each components ,we should use another for loop.
Ex:For I in matrix:
For k in i:
Print(k)
The result would be
2
5
8
5
9
1….
List Methods
Consider the following list
List=[2,5,8,7,6]
1. Adding numbers to the list:
Append()
Ex:List.append(20)
Print(list)
The result would be:
[2,5,8,7,6,20]
*Number 20 would be added at the end of the list
*If we want to add that number in the front or any place of the list we
should use insert method.at the first number we should write the index
Ex:List.insert(0,20)
Print(list)
The result would be:
[20,2,5,8,7,6]
2. Clearing the list:
.clear()
.pop() method would remove the last item on the list
.remove() would clear a given item in the list
3. Existence of a given number in the list
Finding the index:
.index(value u need to find the index of)
Boolean value of the existence of the number:
Print(50 in list)
If the number ‘50’ doesn’t exist in the ‘list’ an error message would be
displayed.
4.Sort()
Sorting the list to ac ascending order
5.reverse()
Reversing a list
6.count()
Counting the special/values in the list
7.floor and ceil method
Used to round the numbers higher and lower
Ex- floor(2.9) – 2
Ceil(2.9) - 3
Download