Python Basics

We know that you already know a lot about Python and it’s capability in the Data Science domain.

I have deliberately put screenshots so that you people have to type these commands to practice the syntax of Python 😛

To make sure everyone is on the same page, we will quickly go through the basics of Python:-

1. print(“Hello World”) – print() command to print anything

2. print(“Hello”+” World”) – Plus(+) operator to add two strings

3. Python will throw an error if you do not follow indentation in your code
4. In Python you do not have to declare any variable by a data type

5. There are three types of numeric types supported in Python:-
a. int
b. float
c. complex

Use the type() command to know the data type

6. Multiple occasions will come when you have to type cast a variable into another data type. Python provides 3 functions for the same:
a. int()
b. float()
c. str()

7. Some basic string operations:

8. Following are the operators used in Python:
a. Arithmetic Operator – These include +,-,*,/,%,**(Exponential),//(Floor division)
b. Assignment Operator – These include =,+=,-=, etc.
c. Comparison Operator – These include >,<,<=,>=,!=,etc.
d. Logical Operator – These include and, or, not.
e. Identity Operator – These include is and is not operator
f. Bitwise operator – These include &, |, ^, ~, << and >>

9. List is mutable and is a collection which is ordered and changeable. By mutable we mean that you can change the content of the List.
A list can contain any data type.

Functions for List:-
a. len() – To get the length of the list
b. append() – To add an element to the end of a list
c. insert() – To add an element at a desired position
d. remove() – To remove specified element
e. pop() – It removes the last index if nothing is specified
f. del – The keyword del removes the specified index
g. clear() – It empties the list

10. Tuples is an unchangeable and ordered collection. List uses a square bracket, whereas tuple uses round brackets. The value of the element of a tuple cannot be changed, thus it is called immutable.

You can completely delete the tuple, but can not add element or delete element from the tuple.

11. Set is another data structure in Python which is unordered and unindexed. Sets are defined by curly brackets.

You can add new items. To add one item you can use add() method, and to add multiple items you can use update() method.

len() function is used to get the length of the set
remove() and discard() functions are used to remove an item from the set. Similarly you can use pop() to remove an item, clear() to empty the set, del to delete the set completely.

We can use the set() constructor to make a set

12. Dictionary is one of the most important and used data structure in Python. It stores the values in key-value pair. It is changeable, unordered and indexed.

The first element is called key and the second is the value. If you have the same key for two different values then it will not get printed. Refer to the example below:

Three important ways to access an element in a dictionary are:-
for x, y in name.items():
  print(x, y)

for x in name.values():

for x in name:

Keep creating tuple, dictionary, and set for the rest of your life 🙂

Keep learning 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *