Share @ LinkedIn Facebook  python, basics
Python Programming Guide For Beginners - Part 2

Learning About Data Types In Python

Every programing lanaguage have their own set of Data Types. Therefore, every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Strings
  • Python Dictionary
  • Python Set

Understanding Python Numbers:

There are interegers, floating number and complex numers in the category of Python Numbers.These data types are defined as int, float, and complex classes.

You can use the type() function to know which class a variable or a value belongs to.

Similarly, the isinstance() function is used to check if an object belongs to a particular class.

Important Points For Data Types In Python

Integer : It can be of any lenght and limited by the memory available.

Floating Point Number: The floating point numbers are accurate up to 15 decimal places.

Complex Number: It is written x + yj, where x is the real part and y is the imaginary part.

Let's see the example to understand the data types in Python.

Program For Data Types

In [3]:
a = 5
print(a, "is of type", type(a))

a = 2.0
print(a, "is of type", type(a))

a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True

Python List

Listis basically an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.

Items separated by commas are enclosed within brackets [ ].

In [1]:
a = [1, 2.2, 'python']

Program To Understand The Use Of Python List Data Type

In [2]:
a = [5,10,15,20,25,30,35,40]

# a[2] = 15
print("a[2] = ", a[2])

# a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])

# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])
a[2] =  15
a[0:3] =  [5, 10, 15]
a[5:] =  [30, 35, 40]

Python Tuples

Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified.

Tuples are used to write-protect data and are usually faster than lists as they cannot change dynamically.

It is defined within parentheses () where items are separated by commas.

Program To Understand The Use Of Tuples

In [5]:
t = (5,'program', 1+3j)

# t[1] = 'program'
print("t[1] = ", t[1])

# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])
t[1] =  program
t[0:3] =  (5, 'program', (1+3j))

Python Strings

String is sequence of Unicode characters.

We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ''' or """.

Program To Understand The Use Of Python Strings

In [6]:
s = "This is a string"
s = '''A multiline
This is a string
A multiline

Python Dictionary

Dictionary is an unordered collection of key-value pairs.

Dictionary is generally used when you are dealing with huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.

In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.

Program To Understand The Application Of Python Dictionary

In [8]:
d = {1:'value','key':2}

print("d[1] = ", d[1]);

print("d['key'] = ", d['key']);
<class 'dict'>
d[1] =  value
d['key'] =  2

Python Set

Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.

Program To Understand The Application Of Python Set

In [9]:
a = {5,2,3,1,4}

# printing set variable
print("a = ", a)

# data type of variable a
a =  {1, 2, 3, 4, 5}
<class 'set'>

Program To Calculate The Are Of Triangle

The variable a, b and c are three sides of a triangle. Then,

Formula Of Triangle

s = (a+b+c)/2 area = √(s(s-a)(s-b)(s-c))

In [1]:
# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
The area of the triangle is 14.70

In the above program you can even take the input ( the values for 3 sides of triangle from the user. Taking input from the user is already explained in "Part-1")

A Simple Program To Reverse The Number

In [11]:
n=int(input("Enter number: "))
print("Reverse of the number:",rev)
Enter number: 23
Reverse of the number: 32

Dolly Solanki  Dolly Solanki