DATA, EXPRESSIONS, STATEMENTS IN PYTHON PROGRAMMING

NishaM41 31 views 23 slides Aug 07, 2024
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

python


Slide Content

DATA, EXPRESSIONS, STATEMENTS UNIT 2

List data type List is an ordered sequence of items Values in the list are called elements/items Lists are created by placing all items inside a square bracket separated by commas Items in a list can be of different data type Lists are mutable

Operations on List Indexing Slicing Concatenation Repetitions Updation , Insertion and Deletion

Creating a List List1=["python",7.79,101,"hello"] List2=["god",6.78,9] Indexing print(List1[0]) python print(List1[2]) 101 Slicing (ending position-1) print(List1[1:3]) [7.79, 101] print(List1[1:]) [7.79, 101, 'hello'] Repetition print(List2*3) ['god', 6.78, 9, 'god', 6.78, 9, 'god', 6.78, 9] Concatenation print(List1+List2) ['python', 7.79, 101, 'hello', 'god', 6.78, 9] Updating the List List1[2]=45 print(List1) ['python', 7.79, 45, 'hello'] Inserting an element List1.insert(2,"program") print(List1) ['python', 7.79, 'program', 45, 'hello'] Removing an element List1.remove(45) print(List1) ['python', 7.79, 'program', 'hello']

Tuple A tuple is a container which holds a series of comma- separated values (items or elements) between parentheses Tuples are immutable It can hold a mix of data types ( int , float, string, list) Tuples are faster than lists Example: tuple1 = (element1, element2, element 3…….element n) tuple1 = (1, 2, 3, 4, 5) tuple2 = (‘a’, ‘b’, ‘c’, ‘d’)

Creating a tuple t=("python",7.79,101,"hello") Indexing print(t[0]) python Slicing print(t[1:3]) (7.79, 101) Concatenation t+("ram",67) ('python', 7.79, 101, 'hello', 'ram', 67) Repetition print(t*2) ('python', 7.79, 101, 'hello', 'python', 7.79, 101, 'hello')

Sets A set is an unordered collection of items. Every element is unique and cannot be changed. A set id created by placing all the items (elements) inside curly braces {}, separated by comma.

my_set ={1,3} print( my_set ) my_set.add (2) # add an element print( my_set ) my_set.update ([2,3,4]) # add multiple elements print( my_set ) my_set.update ([4,5],{1,6,8}) # add list and set print( my_set ) my_set.discard (4) # discard an element in a set print( my_set ) my_set.remove (6) # remove an element print( my_set ) my_set.pop () # pop 1st element from a set print( my_set )

Dictionary Python dictionary is a container of unordered set of elements like list. The elements are surrounded by curly braces {}. Elements in Dictionaries are accessed via keys and not by their position. The values of a dictionary can be of any type. Keys must be immutable data types such as strings, numbers or tuples.

dict ={'Name':'John','SSN':4568,'Designation':'Manager'} print( dict ['Name']) print( dict ['SSN']) print( dict ['Designation']) dict ['Designation']='Senior Manager' dict ['Location']="Delhi" print("After Updating") print( dict ) print( len ( dict ))

Boolean Type  A Boolean type represents special values ‘True’ and ‘False’. They are represented as 1 and 0, and can be used in numeric expressions as value. The most common way to produce a Boolean value is with a relational operator.   Example: 2 < 3 is True

Tokens: A token is the smallest unit of the program Keywords Variables Literals Delimiter Operators

Keywords Keywords are reserved words they have a predefined meanings in Python Python has 33 keywords

Variables A variable is a memory location where a programmer can store a value. Example: roll_no , amount, name etc. No prior declaration is required. The interpreter allocates the memory on the basis of the data type of a variable.

Rules of naming variables Variable name must begin with only letter (a-z, A-Z) or underscore (_) Keywords cannot be a variable No special symbols allowed No blank spaces are allowed between variable names Variable names are case sensitive

Literals A literal is a notation that represents a fixed value Numeric Literals They Consist of digits (0-9) with an optional (+ or -) possibly a decimal point Examples of integer literals Floating point Literals Range of the numeric literal 10 -308 to 10 308 10 2500 -4500 +200

Arithmetic Overflow error a=1.4e200 b=2.1e200 c=a*b print(c)

String Literals String Literals represent sequence of characters Example: “Hello” It can be ‘ (single quote) or double quotes (“ ”)

DELIMITERS Delimiters are symbols that perform three special roles in Python like grouping, punctuation, and assignment/ binding of objects to names. Table presents all 24 of Python’s delimiters. Delimiters of Python Delimiters Classification ( ) [ ] { }     Grouping . , : ; @       Punctuation = +=  = *= /= //= %= **= Arithmetic assignment/ binding &= != ^= <= >>=       Bit wise assignment/ binding

Tuple Assignment An assignment to all of the elements in a tuple using single assignment statement The left side is a tuple of variables; the right side is a tuple of values. Naturally, the number of variables on the left and the number of variables on the right have to be the same

>>> T1 = (10, 20, 30) >>> T2 = (100,200,300,400) >>> print (T1) (10, 20, 30) >>> print (T2) (100, 200, 300, 400) >>> T1, T2=T2, T1 >>> print (T1) (100, 200, 300, 400) >>> print (T2) (10, 20, 30)

Comments Comments make the program easily understandable by the programmer and non-programmer who are seeing the code Comment statements are non-executable statements so they are ignored while program execution They have no effect on the program results Example: # swap.py # Swapping the values of two variable program # written by G.Sugitha , April 2018 Another way of doing comments is to user triple quotes “”” or ‘’’ Example ‘’’ This is also a Perfect example for Multi-line comments’’’
Tags