Arithmetic Operations in Python
Python adds, subtracts, multiplies and divides integers just like general mathematics, where the specific signs are known as operators such as " +, -, , / ".
The new arithmetic operations in python are floor division and modulus.
Floor Division
5//2 gives 2 since the floor value of 2.5 is 2.
MODULUS
The answer to 5%2 is 1 as dividing 5 by 2 gives 1 as the remainder and hence the 8%2 will be 0.
STRING SLICING
The index of lists and strings starts from 0. If we want to get a particular character from a string we can use string slicing as shown above.
Slicing can also be used to print the entire string using steps and it can also be used to reverse a string shown below.
LISTS
Adding elements to a list
USING EXTEND
We can change specific elements in list using list indexing.
DICTIONARIES
Dictionaries behave as key values pairs. They hold values through keys.
Dictionaries can hold any data types as items
TUPLES
Tuples are immutable unlike lists. This is the main difference between the two
SETS
Sets are a collection of unique elements.
FOR and WHILE Loops in Python
Loops print elements in a sequence. The difference between for and while loop is that if we do not know how many times the loop will run we use for loops and if the number of loops in fixed we can use while loop:
FUNCTIONS
Built in MAP Function
LAMBDA EXPRESSION
Helps to get rid of defining the entire function.
FILTER
Filters out elements from a sequence.
Filters out the odd numbers from seq in the above case.
Top comments (0)