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

Python M2 2 (1)

Uploaded by

basuk5795
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python M2 2 (1)

Uploaded by

basuk5795
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Variables, Expressions and Statements

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

Data types distinguish data items according to their characteristics and

structure.
Data types represent the value type that helps understand what operations

can perform on a particular piece of data.


Python programming relies on objects, where data types are considered

classes, and variables are instances (objects) of these classes.


Data types
Python Data Types

Numeric Dictionary Boolean Set Sequence type String

Integer
Lists

Float
Tuple

Complex No.
Range
Introduction
 The following are the standard or built-in data types in Python:

Numeric - int, float, complex

String - str

Sequence Type - list, tuple, range

Boolean - bool

Set - set, frozenset

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 :

 Lists are used to store multiple items in a single variable.


 Lists are one of 4 built-in data types in Python used to store collections of data, the
other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
 Lists are created using square brackets:
 List items are ordered, changeable, and allow duplicate values.
 List items are indexed, the first item has index [0], the second item has index [1] etc
 List items can have any data type
Ex: list = [ 'abcd', 786 , 2.23, 'Scholar-Hat', 70.2 ]
print (list[0]) # Prints first element of the list

OUTPUT: abcd
Python Tuple
 A tuple in Python is an ordered list of elements, just like a list.

 Tuples are not changeable, which is the only difference.

 Once created, tuples cannot be changed.

 In Python, items of a tuple are stored using parentheses ( ).

 In Python, we utilize the index number to retrieve tuple items, just like with lists.

Ex: tuple = ( 'abcd', 786 , 2.23, 'Scholar-Hat', 70.2 )

print (tuple) # Prints the complete tuple

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.

Ex: list (range(1,5)) – start, stop

List (range(1,20,2)) –start, stop, step


Dictionary Data Type
 A dictionary in Python is an unordered collection of data values, used to store data
values like a map
 Unlike other Data Types that hold only a single value as an element, a Dictionary
holds a key value pair.
 Key-value is provided in the dictionary to make it more optimized.
 Each key-value pair in a Dictionary is separated by a colon : , whereas each key is
separated by a ‘comma’.
Dictionary Data Type
 Dictionary can be created by placing a sequence of elements within curly {} braces, separated by
‘comma’.
 Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be
repeated and must be immutable.
 The dictionary can also be created by the built-in function dict().
 An empty dictionary can be created by just placing it in curly braces{}.
 Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated
distinctly.

 student = {"name": "Alice", "age": 25, "grades": [85, 90, 88]}


 # Add new entry
 student["major"] = "Computer Science“
 print
Set Data Type in Python
 In Python, a Set is an unordered collection of data types that is iterable, mutable,
and has no duplicate elements.
 The order of elements in a set is undefined though it may consist of various
elements.
 Curly brackets {}are used to define sets.
Ex: my_set = {1, 2, 3, 4, 5}
print(my_set)
True and 1 are considered same
False and 0 are considered same
Practical 2 –Type function

Module 2
Practical 2 –Type function- type()

A value is one of the basic things in a program.

It may be like 2, 10.5, “Hello” etc.

Each value in Python has a type.

Type of 2 is integer;

type of 10.5 is floating point number;

“Hello” is string etc.


Practical 2 –Type function
The type of a value can be checked using type function as shown
below –
>>> type("hello")
<class ‘str’> #Output

>>> 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

Conversion of Decimal to Binary


Conversion of Binary to Decimal
Octal Number system to Decimal number
system
HexaDecimal System
Hex Decimal Hex Decimal
Value Value
0 0 8 8
1 1 9 9
2 2 10 A
3 3 11 B
4 4 12 C
5 5 13 D
6 6 14 E
7 7 15 F
HexaDecimal to Decimal system
String data type
Welcome to Bangalore
I am learning python
This helps me solve problems using Big data
It is user friendly

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))

You might also like