Introduction To Programming Using Python
Introduction To Programming Using Python
using Python
Python Data Types
In this Notes, you will learn about different data types you can
use in Python.
a = 2.0
print(a, "is of type", type(a))
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
Output
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True
Python List
List is 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.
Declaring a list is pretty straight forward. Items separated by
commas are enclosed within brackets [ ].
a = [1, 2.2, 'python']
# a[2] = 15
print("a[2] = ", a[2])
Output
a[2] = 15
a[0:3] = [5, 10, 15]
a[5:] = [30, 35, 40]
a = [1, 2, 3]
a[2] = 4
print(a)
Output
[1, 2, 4]
Python Tuple
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.
# Generates error
# Tuples are immutable
t[0] = 10
Output
t[1] = program
t[0:3] = (5, 'program', (1+3j))
Traceback (most recent call last):
File "test.py", line 11, in <module>
t[0] = 10
TypeError: 'tuple' object does not support item assignment
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 """.
s = "This is a string"
print(s)
s = '''A multiline
string'''
print(s)
Output
This is a string
A multiline
string
Just like a list and tuple, the slicing operator [ ] can be used with
strings. Strings, however, are immutable.
s = 'Hello world!'
# s[4] = 'o'
print("s[4] = ", s[4])
# s[6:11] = 'world'
print("s[6:11] = ", s[6:11])
# Generates error
# Strings are immutable in Python
s[5] ='d'
Output
s[4] = o
s[6:11] = world
Traceback (most recent call last):
File "<string>", line 11, in <module>
TypeError: 'str' object does not support item assignment
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.
a = {5,2,3,1,4}
Output
a = {1, 2, 3, 4, 5}
<class 'set'>
a = {1,2,2,3,3,3}
print(a)
Output
{1, 2, 3}
Python Dictionary
Dictionary is an unordered collection of key-value pairs.
It is generally used when we have a huge amount of data.
Dictionaries are optimized for retrieving data. We must know the
key to retrieve the value.
We use key to retrieve the respective value. But not the other way
around.
d = {1:'value','key':2}
print(type(d))
# Generates error
print("d[2] = ", d[2]);
Output
<class 'dict'>
d[1] = value
d['key'] = 2
Traceback (most recent call last):
File "<string>", line 9, in <module>
KeyError: 2
Conversion between data types
We can convert between different data types by using different
type conversion functions like int(), float(), str(), etc.
>>> float(5)
5.0
Conversion from float to int will truncate the value (make it closer
to zero).
>>> int(10.6)
10
>>> int(-10.6)
-10
>>> float('2.5')
2.5
>>> str(25)
'25'
>>> int('1p')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1p'
>>> set([1,2,3])
{1, 2, 3}
>>> tuple({5,6,7})
(5, 6, 7)
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
To convert to dictionary, each element must be a pair:
>>> dict([[1,2],[3,4]])
{1: 2, 3: 4}
>>> dict([(3,26),(4,44)])
{3: 26, 4: 44}