Python M2 2 (1)
Python M2 2 (1)
Module 2
Syllabus
Variables, Expressions and Statements: Python installation data
types: Int, float, Boolean, string, and list; variables, expressions,
statements, precedence of operators, comments; modules, function and
its use, flow of execution, parameters and arguments.
INTRODUCTION
Python has many data types, some of which are built-in
structure.
Data types represent the value type that helps understand what operations
Integer
Lists
Float
Tuple
Complex No.
Range
Introduction
The following are the standard or built-in data types in Python:
String - str
Boolean - bool
Dictionary
Numeric data type
The numeric data type in Python represents the data that has a numeric value. A
numeric value can be an integer, decimal, complex number and long integer.
These values are defined as Python int, Python float, and Python complex classes
in Python.
Integers – This value is represented by int class. It contains positive or negative
whole numbers (without fractions or decimals). In Python, there is no limit to
how long an integer value can be.
Float – This value is represented by the float class. It is a real number with a
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 – A complex number is represented by a complex class. It is
specified as (real part) + (imaginary part)j. For example – 2+3j
String Data Type
A group of one or more characters enclosed in a single quotes (‘ ‘) , double quotes (”
“), or triple quote (‘ ‘ ‘ ‘ ‘ ‘) is called a string in python.
A character in Python is just a string with a length of one
The str class is used to represent it.
Creating String
Strings in Python can be created using single quotes, double quotes, or even triple
quotes and also multi line strings
Ex1: Welcome to the Geeks World
Ex2: I’m a Geek
Ex3: I'm a Geek and I live in a world of “Geeks“
Ex 4: Geeks
For
Life
Boolean Data Type
Data type with one of the two built-in values, True or False.
It is used to evaluate conditions and make logical decisions in a program.
Boolean objects that are equal to True are truthy (true), and those equal to False
are falsy (false).
However non-Boolean objects can be evaluated in a Boolean context as well
and determined to be true or false.
It is denoted by the class bool.
Ex: a = True
print(a)
type(a)
Sequence Data Type
The sequence Data Type in Python is the ordered collection of similar or
different data types.
Sequences allow storing of multiple values in an organized and efficient
fashion. There are several sequence types in Python
• Python List
• Python Range
• Python Tuple
Python List :
OUTPUT: abcd
Python Tuple
A tuple in Python is an ordered list of elements, just like a list.
In Python, we utilize the index number to retrieve tuple items, just like with lists.
print (tuple[1:3]) # Prints elements of the tuple starting from 2nd till
3rd
Python Range
Range() in Python is a built-in function that returns a series of numbers that begin at
0 and increase by 1 until they reach a predetermined number.
Utilizing a for and while loop in python, we use the range() method to produce a
series of numbers.
Module 2
Practical 2 –Type function- type()
Type of 2 is integer;
>>> type(3)
<class ‘int’> #Output
>>> type(10.5)
<class ‘float’> #Output
>>> type("15")
<class ‘str’> #Output
NUMERICAL
>>> print(24656354687654+2)
24656354687656 #Output
>>> print(20)
20 #Output
>>> print(0b10)
2 #Output
>>> print(0B10)
2 #Output
>>> print(0X20)
32 #Output
>>> 0b10
2 #Output
>>> a=10
>>> print(a)
Decimal number system
Binary number system
x= Hello
y = How are you
Hello, How are you – desired out put
Boolean Data Type
x=5
y=10
greater_than= x>y
lesser_than = x<y
equal_to = x==y
not_equal_to = x!=y
print("greater than:",greater_than)
print("lesser than:",lesser_than)
print("equal to:",equal_to)
print("not equal to:", not_equal_to)
Boolean Data Type – saving as .doc
%%capture cap
x=5
y=10
greater_than= x>y
lesser_than = x<y
equal_to = x==y
not_equal_to = x!=y
print("greater than:",greater_than)
print("lesser than:",lesser_than)
print("equal to:",equal_to)
print("not equal to:", not_equal_to)
with open(‘output_bool.doc’,’w’) as file:
file.write(cap.stdout)
List
mylist = ["apple", "banana", "cherry"]
print(list1) #print
print(list1(0)) #index
Print(len(list1)) #Lenght
Create a list with all data types
list1 = ["abc", 34, True, 40, "male"]
print(type(list1))
Set
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
thisset = {"apple", "banana", "cherry", True, 1, 2}
print(thisset)
thisset = {"apple", "banana", "cherry"}
print(len(thisset))