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

UNIT-1 Input_Output and Data_Types

The document provides an overview of Python programming focusing on input/output operations and data types. It explains how to use the input() function to read user input, the importance of data types, and how to convert input from strings to integers or floats. Additionally, it covers the structure of Python code using indentation and describes various data types including numeric, sequence, lists, and tuples.

Uploaded by

bharath.vfstr
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)
5 views

UNIT-1 Input_Output and Data_Types

The document provides an overview of Python programming focusing on input/output operations and data types. It explains how to use the input() function to read user input, the importance of data types, and how to convert input from strings to integers or floats. Additionally, it covers the structure of Python code using indentation and describes various data types including numeric, sequence, lists, and tuples.

Uploaded by

bharath.vfstr
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/ 73

Python Programming for Problem Solving

Unit – 1: Input/Output and Data Types

Mr. N. Bharath Kumar


Assistant Professor
Department of EEE

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• input() is used for standard input operations (to read data from
the user).
• input() function is used to read data from the standard input
device (keyboard) – during runtime.
Syntax:
Variable_name = input(“prompt”)

where prompt is an optional string that is displayed on the screen


at the time of reading input (Use the prompt parameter to write a
message before the input).
Example:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:

Example:

a = input(“enter a value = ”)
print(type(a))
a = int(a)
print(type(a))
a = float(a)
print(type(a))

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:

Example:
Output:
a = input(“enter a value = ”)
print(type(a))
a = int(a)
print(type(a))
a = float(a)
print(type(a))

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• Python takes all the input as a string input by default .
• To convert it to any other data type we have to convert the input
explicitly.
• int(), float() functions are used to convert this string into integer or
floating-point number.
Syntax:

Variable_name = data_type(input(“prompt”))

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• Reading int, float values during runtime.
Example1:
a = int(input(“enter a value = ”))
print(type(a))

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• Reading int, float values during runtime.
Example1: Output1:
a = int(input(“enter a value = ”))
print(type(a))

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• Reading int, float values during runtime.
Example1: Output1:
a = int(input(“enter a value = ”))
print(type(a))

Output2:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• Reading int, float values during runtime.
Example1:
a = int(input(“enter a value = ”))
Print(a)
print(type(a))

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• Reading int, float values during runtime.
Example1: Output1:
a = int(input(“enter a value = ”))
Print(a)
print(type(a))

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Input:
• Reading int, float values during runtime.
Example1:
Output1:
a = int(input(“enter a value = ”))
Print(a)
print(type(a))
Output2:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Input and Output

Output:
• print() function is used to output data to the standard output
device (screen).
Syntax:
print(variable_name)
Example:

a = 5
print(a)
print(‘value of a is ’, a)

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Indentation in python

A block is a group of statements in a program or script.

Most of the programming languages like C, C++, and Java use braces
{ } to define a block of code. Python, however, uses indentation.

Python does not use braces({}) to indicate


blocks of code for class and function
definitions or flow control.
 In python Blocks of code are denoted by
a line indentation.
Generally, four whitespaces are used for
indentation and are preferred over tabs.

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Indentation in python

 Python programs get structured through indentation, i.e. code


blocks are defined by their indentation.
All statements with the same distance to the right belong to the
same block of code.
If a block has to be more deeply nested, it is simply indented
further to the right.
Leading whitespace (spaces and tabs) at the beginning of a logical
line is used to compute the indentation level of the line, which in
turn is used to determine the grouping of statements.

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Indentation in python

Example:

a=input("enter a")
b=input("enter b")
if a>b:
print('a is big')
print("hi")
else:
print("b is big")

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python

• In programming, the data type is an important concept.


• Variables can store data of different types, and different types can
do different things.
• Data types are the classification or categorization of data items.
• It represents the kind of value that tells what operations can be
performed on a particular data.
• Since everything is an object in Python programming, data types
are actually classes and variables are instances (objects) of these
classes.
• Note – type() function is used to determine the type of data type.

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python

• Python has the following data types built-in by default, in these


categories:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python

Numeric Datatypes:
• In Python, numeric data type represents the data that
has a numeric value.
• Numeric values can be integers, floating numbers, or
even complex numbers.
• These values are defined as
• int
• Float
• complex

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Numeric Datatypes:
• Integers – This value is represented by int class. It contains
positive or negative whole numbers (without fraction or decimal).
In Python, there is no limit to how long an integer value can be.
• Float – This value is represented by float class. It is a real number
with floating-point representation. It is specified by a decimal
point. Optionally, the character e or E followed by a positive or
negative integer may be appended to specify scientific notation.
• Complex Numbers – Complex number is represented by complex
class. It is specified as (real part) + (imaginary part)j. For example
– 2+3j

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Numeric Datatypes:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
• In Python, the sequence is the ordered collection of
similar or different data types.
• Sequences allow storing multiple values in an organized
and efficient fashion.
• There are several sequence types in Python.
• String
• List
• Tuple
• Range

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:
• In Python, Strings are arrays of bytes representing Unicode
characters.
• A string is a collection of one or more characters put in a single
quote, double-quote, or triple quote.
• In python there is no character data type, a character is a string
of length one.
• It is represented by an str class.
• Strings in Python can be created using single quotes or double
quotes or even triple quotes.
Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
String:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:
• Lists are just like the arrays, declared in other languages which is
an ordered collection of data.
• It is very flexible as the elements in a list do not need to be of the
same type.
• Lists in Python can be created by just placing the sequence inside
the square brackets [ ].
• It is represented by a list class.

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Lists:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:
• Just like a list, the tuple is also an ordered collection of Python
objects.
• The only difference between tuple and list is that tuples are
immutable i.e. tuples cannot be modified after being created.
• In Python, tuples are created by placing a sequence of values
separated by a ‘comma’ with or without the use of parentheses for
grouping of the data sequence.
• Tuples can contain any number of elements and of any datatype
(like strings, integers, lists, etc.).
• It is represented by a tuple class.

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Sequence Datatypes:
Tuple:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Mapping Datatypes:
Dictionary:
• Dictionary in Python is an unordered collection of data values.
• Dictionaries are used to store data values in key: value pairs.
• In Python, a Dictionary can be created by placing a sequence of
elements within curly { } braces, separated by ‘comma’.
• Values in a dictionary can be of any data type and can be
duplicated, whereas keys can’t be repeated and must be
immutable.
• It is represented by a dict class.

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Mapping Datatypes:
Dictionary:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Mapping Datatypes:
Dictionary:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Mapping Datatypes:
Dictionary:

Keys of the dictionary

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Mapping Datatypes:
Dictionary:

values of the dictionary

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Mapping Datatypes:
Dictionary:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Set Datatypes:
Sets:
• Python Set is the unordered collection of the data type.
• It is iterable, mutable (can modify after creation), and has unique
elements.
• In set, the order of the elements is undefined; it may return the
changed sequence of the element.
• The set is created by using a built-in function set(), or a sequence
of elements is passed in the curly braces and separated by the
comma. It can contain various types of values.
• It is represented by a set class.

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Set Datatypes:
Sets:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Set Datatypes:
Sets:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Set Datatypes:
Sets:

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Boolean Datatype:
• Boolean type is one of the built-in data types provided by
Python, which are defined by the True or False keywords.
• Generally, it is used to represent the truth values of the
expressions.
• It is represented by a bool class.
>>> a = True
>>> type(a) Output:
<class 'bool'>
>>> b = False <class 'bool'>
>>> type(b)

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Boolean Datatype:
• In programming you often need to know if an expression is True
or False.
• You can evaluate any expression in Python, and get one of two
answers, True or False.
• When you compare two values, the expression is evaluated and
Python returns the Boolean answer

>>> print(10 > 9) Output:


>>> print(10 == 9) True
>>> print(10 < 9) False
False

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Data types in Python
Boolean Datatype:
• The bool() function allows you to evaluate any value, and give you
True or False in return
• Almost any value is evaluated to True if it has some sort of content.
• Any string is True, except empty strings.
• Any number is True, except 0.
• Any list, tuple, set, and dictionary are True, except empty ones.

>>> bool("abc")  True


>>> bool("")  False
>>> bool(123)  True
>>> bool(())  False
>>> bool(False)  False
>>> bool([])  False
>>> bool(None)  False
>>> bool({})  False
>>> bool(0)  False
Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Boolean Operations

• Boolean Operations are simple arithmetic of True and False


values. These values can be manipulated by the use of Boolean
operators which include AND, Or, and NOT.
• Common Boolean operations are:
and
or
not
== (equivalent)
!= (not equivalent)

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Boolean Operations

• Boolean Operations are simple arithmetic of True and False


values. These values can be manipulated by the use of Boolean
operators which include AND, Or, and NOT.
• Common Boolean operations are:
and Ex:
A = True
or B = False
not print(A or B)
print(A and B)
== (equivalent) print(not A)
!= (not equivalent) print(not B)
print(A == B)
print(A != B)
Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Boolean Operations

• Boolean Operations are simple arithmetic of True and False


values. These values can be manipulated by the use of Boolean
operators which include AND, Or, and NOT.
• Common Boolean operations are:
and Ex:
A = True
or B = False
not print(A or B)
print(A and B)
== (equivalent) print(not A)
!= (not equivalent) print(not B)
print(A == B)
print(A != B)
Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Boolean Logic

• Booleans are commonly used in the code to make it behave


differently. Boolean can be used in conjunction with conditional
statements to make it simpler. In many cases multiple conditions are
needed to evaluate, for this purpose, AND and OR keywords are
used. The AND returns True only if both the conditions are true,
while OR returns true if any one of the conditions is true.
Ex:
num = 10
if num > 1 and num < 10 :
print("less then 10")
elif num >10 or num == 10 :
print("geater or equal to 10")
else :
print("not in range")

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Boolean Logic

• Booleans are commonly used in the code to make it behave


differently. Boolean can be used in conjunction with conditional
statements to make it simpler. In many cases multiple conditions are
needed to evaluate, for this purpose, AND and OR keywords are
used. The AND returns True only if both the conditions are true,
while OR returns true if any one of the conditions is true.
Ex:
num = 10
if num > 1 and num < 10 :
print("less then 10")
elif num >10 or num == 10 :
print("geater or equal to 10")
else :
print("not in range")

Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE
Department
N BharathofKumar
Electrical and Electronics Engineering N BHARATH of
Department KUMAR
EEE

You might also like