Python Data Types
Python Data Types
HANDOUT #03
ISHAN VIRANTHA
1
3. Python Data Types
In Python When defining the variable no need to sfecifyt the data going to be store in the variable
like C programing.
2
Python Variable Names
Following table has examples of legal variable names. You can name a variable anything as
long as it obeys the following three rules:
1. It can be only one word.
2. It can use only letters, numbers, and the underscore (_) character.
3. It can’t begin with a number.
Type help() in python shell and type keywords to ge the list of key word in python
3
Scope of a Python Variable
Variables can only be seen or accessible within their scope. Two variable scopes are classified in
Python:
Local variables are defined and only used by the calling function. They are not recognized by
the main program or in any other calling functions.
Global variables are common to most part of the code. They are recognized everywhere and can
be accessed by any functions.
4
3.2 Python Data types
The data stored in memory can be of many types. For example, a person's age is stored as a
numeric value and his or her address is stored as string. Python has various standard data types,
can be listed as follows
String (Str)
List (list)
Numbers
Tuple (tuple)
Integer (int)
Dictionary (dict)
Float (float) (int)
Set (set)
Complex
String (Str)
Strings in Python are identified as a contiguous set of characters represented in the quotation
marks. Python allows either pair of single or double quotes.
1. Creating string
5
2. Accessing Values in Strings.
In python all string can acces through the variable nameand the part of string can acces through
indexing
Assume string variable a holds 'Hello' and variable b holds 'Python', then
6
4. String formating Operator (%)
Sring format operater is used when need to insert the value of a variable, or expression into a
string, For example, you can insert the value of a Decimal value into a string to display it to the
user as a single string:
The order of inserting variable value in a string can change using positional order or key word
order. See the following example.
7
Here is the list of complete set of symbols which can be used along with % (old style)
8
Old new output
Basic formatting
'%s %s' % ('one', 'two') '{} {}'.format('one', 'two') one two
'{:_<10}'.format('test') test______
N/A
'{:^10}'.format('test') xxxtestxxx
N/A
Numbers
'%d' % (42) '{:d}'.format(42) 42
5. Escap character
Escape
Description Example Output
Sequence
\\ Prints Backslash print ("\\") \
Prints single-
\’ print ("\'") '
quote
Pirnts double
\" print ("\"") "
quote
Bell alert
\a sounds ( eg. print ("\a") (bell soud)
xterm )
9
Escape
Description Example Output
Sequence
Removes previous print ("ab" + "\b" +
\b ac
character "c")
ASCII formfeed ( hello
\f print ("hello\fworld")
FF ) world
ASCII linefeed ( hello
\n print ("hello\nworld")
LF ) world
ASCII horizontal
\t tab (TAB). print ("\t* hello") * hello
Prints TAB
Numbers
Number data types store numeric values. Number objects are created when you assign a value to
them.Python supports three different numerical types –
10
List (list)
Lists are the most versatile of Python's compound data types. A list contains items separated by
commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C.
One of the differences between them is that all the items belonging to a list can be of different
data type.
In Python programming, a list is created by placing all the items (elements) inside a square
bracket [ ], separated by commas.
It can have any number of items and they may be of different types (integer, float, string etc.).
Also, a list can even have another list as an item. This is called nested list.
11
8. How to access elements from a list?
We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements
will have index from 0 to 4. The index must be an integer.
Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to
the second last item and so on.
12
9. How to slice lists in Python?
We can access a range of items in a list by using the slicing operator (colon).
List are mutable, meaning, their elements can be changed unlike string or tuple. We can use
assignment operator (=) to change an item or a range of items.
13
Output will be as follows
We can add one item to a list using append() method or add several items using extend()
method.
We can also use + operator to combine two lists. This is also called concatenation.
14
Furthermore, we can insert one item at a desired location by using the method insert() or insert
multiple items by squeezing it into an empty slice of a list.
We can delete one or more items from a list using the keyword del. It can even delete the list
entirely.
15
We can use remove() method to remove the given item or pop() method to remove an item at
the given index.
The pop() method removes and returns the last item if index is not provided. This helps us
implement lists as stacks (first in, last out data structure).
We can test if an item exists in a list or not, using the keyword in.
16
Example program 3.6 output
NOTE1 NOTE2
IN C: In C:
for (i = 0; i < n; i++) for(int i=0; i<9; i+=2)
{
In python3: dosomething(i);
}
for i in range(n):
In python3:
Tuple (tuple)
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of
values separated by commas. Unlike lists, however, tuples are enclosed within parenthesis.
The main difference between lists and tuples is- Lists are enclosed in brackets ( [ ] ) and their
elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated. Tuples can be thought of as read-only lists. For example-
17
Difference between Tuples and List
18
As you can see above, we can also create a dictionary using the built-in function dict().
Dictionary uses keys accsess the data . Key can be used either inside square brackets or with
the get() method.
We can add new items or change the value of existing items using assignment operator.If the
key is already present, value gets updated, else a new key: value pair is added to the dictionary.
19
Tel:- 071 429 39 50
20