DEV Community

Cover image for BASIC PYTHON
Nazihah Islam
Nazihah Islam

Posted on

BASIC PYTHON

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

Image description

5//2 gives 2 since the floor value of 2.5 is 2.

MODULUS

Image description

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

Image description

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.

Image description

Slicing can also be used to print the entire string using steps and it can also be used to reverse a string shown below.

Image description

LISTS

Adding elements to a list

USING APPEND:
Image description

USING EXTEND

Image description

We can change specific elements in list using list indexing.

Image description

DICTIONARIES

Dictionaries behave as key values pairs. They hold values through keys.

Image description

Dictionaries can hold any data types as items

Image description

TUPLES

Image description

Tuples are immutable unlike lists. This is the main difference between the two

SETS

Sets are a collection of unique elements.

Image description

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:

Image description

FUNCTIONS

Image description

Image description

Built in MAP Function

Image description

LAMBDA EXPRESSION

Helps to get rid of defining the entire function.

Image description

FILTER

Filters out elements from a sequence.

Image description

Filters out the odd numbers from seq in the above case.

Splitting a string into list

Image description

IN and BOOLEANs

Image description

TUPLE Unpacking

Image description

Top comments (0)