0% found this document useful (0 votes)
13 views

Sessio-3 Knowledge of Data Types and Debugging

The document discusses the key features and data types in the Python programming language, including that Python is an interpreted, object-oriented, and platform independent language that uses dynamic typing and supports numeric, string, list, tuple, dictionary, set and boolean data types which can be mutable or immutable.

Uploaded by

Sahith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Sessio-3 Knowledge of Data Types and Debugging

The document discusses the key features and data types in the Python programming language, including that Python is an interpreted, object-oriented, and platform independent language that uses dynamic typing and supports numeric, string, list, tuple, dictionary, set and boolean data types which can be mutable or immutable.

Uploaded by

Sahith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

By – Amresh Tiwari (SGEI)

Simple and interactive


Features of Python Language
Platform independent

Case sensitive

Object oriented

Interpreted language

Uses variable without declaration


By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)
Smallest individual element
/unit of a program is called as
Token.
Tokens

Everything you see inside a


program is a token.
1. keywords(Reserve words)

2. Identifiers (Names that the programmer


Types Of defines) eg.- Variable

Token 3. Literals(Constants)

4. Operators(Special Symbols that operate on data


and produce results)

5. Delimiters (Grouping, punctuation, and


assignment/binding symbols)
Literal or Constant
Variable

Operator

Note - A program is defined as a


systematic collection of a token.
Delimeter

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


In programming, a variable is a value that can
change, depending on conditions or on information
passed to the program.

• Variables are the names you give to


computer memory locations

• which are used to store values in a


computer program.

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Data type identifies the type of data
values a variable can hold and the
operations that can be performed on
that data.
Variables can hold values of different
data types.

Python Data
Python is a dynamically typed language
Types hence we need not define the type of
the variable while declaring it.

The interpreter implicitly binds the value


with its type.

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)
By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)
Numbers
Python provides
various standard
data types that String

define the storage


method on each of List
them.

The data types Tuple

defined in Python
are- Dictionary

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


int (signed integers like 10, 2, -29, etc.)

Python long (long integers used for a higher


supports 4 range of values like 908090800L, -
0x1929292L, etc.)
types of
numeric data. float (float is used to store floating point
numbers like 1.9, 9.902, 15.2, etc.)

complex (complex numbers like 2.14j,


2.0 + 2.3j, etc.)
Boolean data type
• Boolean data type (bool) is a subtype of integer.
• It is a unique data type, consisting of two constants, True and
False.
• Boolean True value is non-zero, non-null and non-empty.
Boolean False is the value zero.
• Eg. >>> var1 = True
>>> type(var1)
<class 'bool'>
By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)
Sequence
• A Python sequence is an ordered collection of items, where
each item is indexed by an integer.
• The three types of sequence data types available in Python are
Strings, Lists and Tuples.
• Note:

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


The string can be defined as the
sequence of characters represented
in the quotation marks.

String
In python, we can use single,
double, or triple quotes to define a
string.

str1 = ”Good Morning” #string str1


Eg.
str2 = ' how are you’ #string str2

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


List is a sequence of items
separated by commas and the
items are enclosed in square
brackets [ ].

List Example #To create a list


>>> list1 = [5, 3.4, "New Delhi", "20C", 45]
#print the elements of the list list1
>>> print(list1)
[5, 3.4, 'New Delhi', '20C', 45]

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Tuple is a sequence of items
separated by commas and items are
enclosed in parenthesis ( ).

This is unlike list, where values are

Tuple enclosed in brackets [ ]. Once


created, we cannot change the
tuple.

#create a tuple tuple1


>>> tuple1 = (10, 20, "Apple", 3.4, 'a')
Eg. #print the elements of the tuple tuple1
>>> print(tuple1)
(10, 20, "Apple", 3.4, 'a')

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Set
• Set is an unordered collection of items separated by commas and the items are enclosed in
curly brackets { }.
• A set is similar to list, except that it cannot have duplicate entries.
• Once created, elements of a set cannot be changed.
• Example: #create a set
>>> set1 = {10,20,3.14,"New Delhi"}
>>> print(type(set1))
<class 'set'>
>>> print(set1)
{10, 20, 3.14, "New Delhi"}
#duplicate elements are not included in set
>>> set2 = {1,2,1,3}
>>> print(set2)
{1, 2, 3}

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


None
• None is a special data type with a single value.
• It is used to signify the absence of value in a situation.
• None supports no special operations, and it is neither same as False
nor 0 (zero).
• Example
>>> myVar = None
>>> print(type(myVar))
<class 'NoneType'>
>>> print(myVar)
None

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Mapping
• Mapping is an unordered data type in Python.
• Currently, there is only one standard mapping data type in
Python called dictionary.

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Dictionary
• Dictionary in Python holds data items in key-value pairs.
• Items in a dictionary are enclosed in curly brackets { }.
• Dictionaries permit faster access to data.
• Every key is separated from its value using a colon (:) sign. The
key : value pairs of a dictionary can be accessed using the key.
• The keys are usually strings and their values can be any data
type.
• In order to access any value in the dictionary, we have to
specify its key in square brackets [ ].

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Dictionary…
Example
#create a dictionary
>>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
>>> print(dict1['Price(kg)'])
120

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Mutable and Immutable Data Types
• Variables whose values can be changed after they are created
and assigned are called mutable.
• Variables whose values cannot be changed after they are
created and assigned are called immutable.
• When an attempt is made to update the value of an immutable
variable, the old variable is destroyed and a new variable is
created by the same name in memory.

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


Mutable and Immutable Data Types

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


• Python enables us to check the type of the
variable used in the program.
• Python provides us the type() function
which returns the type of the variable
passed.

type() • Eg. A=10

function b=“Jai Hind"


c = 10.5
print(type(a));
print(type(b));
print(type(c));
Assignment…
• Which data type will be used to represent the following data
values and why?
a) Number of months in a year
b) Resident of Delhi or not
c) Mobile number
d) Pocket money
e) Volume of a sphere
f) Perimeter of a square
g) Name of the student
h) Address of the student
By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)
Assignment…
• Give the output of the following when num1 = 4, num2 = 3, num3 = 2
a) num1 += num2 + num3
print (num1)
b) num1 = num1 ** (num2 + num3)
print (num1)
c) num1 **= num2 + num3
d) num1 = '5' + '5'
print(num1)
e) num1 = 24 // 4 // 2
print(num1)

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


DEBUGGING
• A programmer can make mistakes while writing a program, and
hence, the program may not execute or may generate wrong
output.
• The process of identifying and removing such mistakes, also known
as bugs or errors, from a program is called debugging.
• Errors occurring in programs can be categorized as:
i) Syntax errors
ii) Logical errors
iii) Runtime errors

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


i) Syntax errors
• Like other programming languages, Python has its own rules
that determine its syntax.
• The interpreter interprets the statements only if it is
syntactically (as per the rules of Python) correct.
• For example, parentheses must be in pairs, so the expression
(10 + 12) is syntactically correct, whereas (7 + 11 is not due to
absence of right parenthesis.

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)


ii) Logical errors
• A logical error is a bug in the program that causes it to behave
incorrectly.
• A logical error produces an undesired output but without abrupt
termination of the execution of the program.
• Logical errors are also called semantic errors as they occur when
the meaning of the program (its semantics) is not correct.
• For example, if we wish to find the average of two numbers 10 and
12 and we write the code as 10 + 12/2, it would run successfully
and produce the result 16. Surely, 16 is not the average of 10 and
12.
By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)
iii) Runtime errors
• A runtime error causes abnormal termination of program while
it is executing.
• Runtime error is when the statement is correct syntactically,
but the interpreter cannot execute it.
• Runtime errors do not appear until after the program starts
running or executing.
• For example, we have a statement having division operation in
the program. By mistake, if the denominator entered is zero
then it will give a runtime error like “division by zero”.
By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)
Exercise
• Categorise the following as syntax error, logical error or
runtime error:
a) 25 / 0
b) num1 = 25; num2 = 0; num1/num2
c) print(“Hello”

By-Amresh Tiwari, Sunbeam Suncity (School & Hostel)

You might also like