Python for beginners part II

Saroj Humagain
4 min readOct 28, 2018

Data types in Python

There are two types of data types in Python. Immutable and mutable.

  1. Immutable: Those data types whose value cannot be changed or modified. Immutable data types are

I. Numbers:
Python can have four types of numerical literals. Integer, long integer, floating numbers, and complex numbers. We don’t have to specify the data types of a variable while we are declaring it. Python automatically converts a number from one data types to another if it needs. Also, we can explicitly convert one data type to another. This can be done using int(), long(), float(), and complex() functions.

II. String:
Strings are anything within a single quote or a double quote. Various operations can be done with string.

str = “Python for beginners”
print(str[0])
output
P
print(str[2:5])
output
tho

There are some predefined set of functions on a string. They are

find(): It returns the position of the string.

str = “Impossible”
str.find(“possible”)
output
2
And if we search for a string that is not a substring it returns -1.

replace(): Replaces one character or string with other.

str = “Played”
str.replace(“ed”, ‘s“)
output
Plays

split(): It creates a split on the basis of a character.

str = “ Python2,Python3”
str.split(‘,’)
output
[“Python2”, “Python3”]

count(): It returns the number of appearances of a particular character in a string.

str = “Programming”
str.count(“g”)
output
2

upper() and lower(): They converts a string from lowercase to uppercase and vice-versa.

max() and min(): They returns a character with a maximum and minimum ASCII value.

III. Tuples
Tuples are a group of values within the parenthesis and since they are immutable data types so the value within the tuples cannot be changed.
for example:

animals = (‘cat’, ‘dog’, ‘ mouse’)

There are some operations that can be done with tuples.
Concatenation: Generally it adds two strings or characters. In tuples, it is done as follows

vow = ('a','e','i','o')
vow += ('u',)
print(vow)
output
('a','e','i','o','u')

Repetition: Replication is the process of duplication the string or character by the given number of time. for eg

vow = ('a','e','i','o',u)
vow*3
output
('a','e','i','o','u','a','e','i','o','u','a','e','i','o','u')

Slicing: Slicing is the process of selecting a certain element or elements from the given tuples.for eg

vow = ('a','e','i','o','u')
vow[1:] #selects the element from index 1 to the last
output
('e','i','o','u')
vow[:3] #selects all the elements from index 0 to the index 3
output
('a',e','i','o')

2. Mutable: Those data types whose value can be changed or modified. Mutable data types are
I. List: A list is a sequence of mutable python objects defined under [ ]. For example, num = [1,3,2,4,22,9]

There are some operation that can be performed with the list. They are described below

Concatenation: Similar to the tuple, it adds two strings or characters.

animals = ["cat", "dog", "rat"]
animals+= ["snake" ,] #comma is mandotory
print(animals)
output
["cat", "dog", "rat", "snake"]

Repetition: Same as in tuple

Slicing:

num = [1,3,5,7,9,0,32]
num[:4] #gives first four elements
output
[1,3,5,7]
num[-1] #gives last element
output
[32]
num[2:] #gives elements from index 2 to last
output
[5,7,9,0,32]

Append: append(value)
It adds its argument as a single element to the end of the list, increasing the size of the list by one.

num = [1,3,5,7,9,0,32]
num.append(15)
print(num)
output
[1,3,5,7,9,0,32,15]

Extend: extend(list)
It concatenates the first list to another list passed with the extend function.

num = [1,3,5,7,9,0,32]
num.extend[15,33,45]
print(num)
output
[1,3,5,7,9,0,32,15,33,45]

Insert: insert(index,value)
We can also insert particular value in a particular index.

num = [1,3,5,7,9,0,32]
num.insert(3,11)
print(num)
output
[1,3,5,11,9,0,32,15,33,45]

II. Dictionary: Dictionaries are the most flexible built-in data types in Python. In this, items are grouped in {}, and the elements or the values are grouped together with a key. The items are stored and fetched by key, instead of by positional offset. Generally, a dictionary is the collection of key-value pairs. For eg

myDict = {1: "cat", 2: "dog"} #Dictionary with the integer key value
myDict = {name: "saroj", 2: "male"} #mixed key value pairs

Some built-in operations on dictionaries:

myDict = {1: "cat", 2: "dog", 3: "snake"}
myDict[2] #accessing the dictionary
output
"dog"
len(myDict) #gives length of dictionary
output
3
myDict.keys() #gives all keys
output
dict_keys([1,2,3])
myDict.values() #gives all values
output
dict_values(["cat","dog","snake"])
myDict.updat({4: "horse"}) # adds one pair at the last
print(myDict)
output
{1: "cat", 2: "dog", 3: "snake",4: "horse"}

III. Sets: A set is an unordered collection of items. Values are grouped inside the { }. And every element in a set is unique and immutable. For eg

mySet = {11,33,55} #is a set
mySet = {11,22,33,22,43} #is not a set 22 cannot be duplicated

There are some operations that can be done with sets. They are mentioned below

mySet1 = {1,3,5,7,6,8}
mySet2 = {2,4,6,8,5,10}
mySet1 | mySet2 #gives union
output
{1,2,3,4,5,6,7,8,10}
mySet1 & mySet2 #gives intersection
output
{5,6,8}
mySet1 - mySet2 #gives differences
output
{1,3,7}
mySet2 - mySet1
output
{2,4,10}

The third part will include flow controls and function in Python.

--

--

Saroj Humagain

I basically write on data science, ML and AI and sometimes random things.