Python programming unit-2 identifiers and keywords.pptx

AmyPrasannaTella1 9 views 40 slides Oct 18, 2025
Slide 1
Slide 1 of 40
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
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40

About This Presentation

Python programming identifiers and keywords


Slide Content

Python programming unit-II

Contents Python identifiers(literals) Reserved keywords Variables Comments Lines and indentations Quotations Assigned values to variables Datatypes in python Mutable vs Immutable

Contents Fundamental Datatypes Number Datatypes Inbuilt functions in Python Datatype conversions Python operators

Python Identifiers: Identifier is a name used to identify a variable, function, class or a module. It can start with letter A to Z (or) a to z, an underscore(_) followed by zero or more letters, underscores and digits(0-9). It doesn’t allow special characters like @,$,% with identifiers. These are case sensitive i.e., Manpower and manpower are two different identifiers.

Examples: Valid examples Invalid examples Abc10d 99 ABC_DE 9abc _ abc X+Y _ For

Reserved Keywords: These are the reserved words used in Python. These names cannot be used as either identifiers or constants or variables. False Await Else Import pass None Break Except In raise True Class Finally Is return And Continue For Lambda try As Def From Nonlocal while Assert Del Global Not with Async Elif If Or yield

How to get list of keywords in Python: >>> import keywords >>> print ( keywords.kwlist )

Variables: Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. It is the name use to declare any value either it can be int, float, complex or any other type. It is created at the moment we assign a value and refers to a memory location. String variable can be declared either by ‘ ’ or “ ”. Eg : a=10 a=“name” a=10.5

Rules for Variables: A variable can have a short name (like x and y) or a more descriptive name (age, carname , total_volume , etc ) A variable name mus t start with a letter or the underscore character. A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores(A-Z,0-9 and _). Variables names are case-sensitive( age,Age and AGE are three different variables).

Comments: it helps the beginners to understand any tougher logics and also to recollect the logic they have written. In c programming comments starts with /* and ends with */. In python comments starts with # Eg : # printing # hello world Eg : ””” it is also an example of multi line comments”””

Lines and Indentations: Most of the programming languages like c, c++, and java use braces {} to define a block of code. Python uses indentation Python doesn’t support braces to indicate block of codes for class and function, definitions or flow control. Block of codes are denoted by line indentation. All the continuous lines indented with same number of spaces would form a block. Python strictly follows indentation rules to indicate the blocks. Incorrect indentation will results indentation error.

Example: if 5>2: Print(“five is greater than two!”) This gives an error as there is an indentation missing. if 5>2 : print(“five is greater than two!”)

Quotations: Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string. The strings created by using single and double quotes are the same. In other words, we can use single and double quotes interchangeably when we declare a string.

Assigned values to variables: Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. eg : n=100 This is read or interpreted as “n is assigned the value 100”. N can be used in a statement or expression and its value will be substituted. Later, if you change the value of n and use it again, the new value will be substituted instead

Data types in Python: Variables can hold values, and every value has a data-type. Python is a dynamically typed language Python provides us the  type()  function, which returns the type of the variable passed. Type represents the kind of value and determines how the value can be used.

Types of datatypes: There are 5 basic types of datatypes as mentioned: Numeric Dictionary Boolean Set and Sequence type

MUTABLE vs IMMUTABLE: Mutable : These are of type list , dict , set   . Custom classes are generally mutable . list=[‘orange’, ‘pink’, ‘yellow’] list[0]= ‘red’ list[-2]= ‘blue’ Immutable:  These are of in-built types like int, float, bool, string, unicode , tuple. In simple words an immutable object can’t be changed after it is created. Eg : tuple1=(0,1,2,3) tuple1[0]=4 Print(tuple1)

Fundamental Datatypes: These datatypes are created by numerical values. Integer Floating number Complex numbers Boolean and Strings

INTEGER: represented as <class ‘int’>. Consists of either positive or negative whole numbers. Python2 version has int, long but python3 version has only int. There is not length of the integer value unlike other datatypes. Eg : a=1234 b=(-4567) c=0 d=(123456789123456789123456788+1)

FLOATING NUMBER: Represented as <class ‘float’>. Generally specified with a decimal point values. It also accepts a scientific notation like a character e/E. Eg : a=4.2 b=4. or .2 c= .4e7 d= 4.2e-4

COMPLEX NUMBERS: Represented as <class ‘complex’>. these are ordered pair i.e., a+bJ or a+bj . Where j is the imaginary number i.e., square root of -1. Here the values are considered as a floating numbers. Eg : a=2-14j b= 2.0+3j

BOOLEAN: Represented as <class ‘bool’>. It is the datatype with two built-in values, “True” or “False”. Non-Boolean objects can also be evaluated in Boolean context. Eg : a=6;b=7 a>b print(type(True)) print(type(False)) print(type(false))

STRINGS: Represented as <class ‘str’>. These are arrays of bytes representing Unicode characters. These are the collection of characters in a ‘’,””,’’’. Here we don’t have any datatype like char in python. How to create a string? How to access the values of a string? M A L L A R E D D Y

Example: How to create a string string1=(“ malla reddy ”) print(“string with the use of double quotes:”) print(string1) 2. How to access elements : string1=(“ malla reddy ”) print(string1[4]) print(string1[-7])

Number data types: We have 4 types in number datatypes: Binary Decimal Octal and Hexadecimal.

Binary: it is a number system with base 2 and computer can understand only binary numbers(0 and 1). Represented as 0b. Decimal: is most widely used with base 10. Represented from 0 to 9. Octal: it is number system with base 8 and represented as 0o. Hexadecimal: a number system with base 16 and represented as 0x.

Inbuilt Functions: The Python built-in functions are defined as the functions whose functionality is pre-defined in Python. The python interpreter has several functions that are always present for use.

Datatype conversions: There are two types of conversions: Implicit: automatic conversion eg : a=5 print(type(a)) 2. Explicit: it requires user involvement. This can be done with the help of int(), str(), float() etc.,

EXPLICIT CONVERSIONS: Converting number to string: using str() function. Eg : a=10 s=str(a) print(s) print(type(s)) Converting string to number: using int(), float() function. Eg : s= ‘50’ n=int(s) f=float(s) print(n) print(f) print(type(n)) print(type(f))

Example: Converting floating point to integer: using int() function. Eg : f=10.0 n=int(f) print(n) print(type(n)) Converting integer to float type: using float() function. Eg : n=10 f=float(n) print(f) print(type(f))

Converting list to a tuple and tuple to a list: using list(), tuple() function. Eg : t=(1,3,2,4) l=[5,6,7,8] T=tuple(l) L=list(t) print(T) print (L) print(type(T)) print(type(L))

Python Operators: The operator can be defined as a symbol which is responsible for a particular operation between two operands Python provides a variety of operators. Arithmetic Operators Comparison Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators and Identity Operators.

Arithmetic Operators: Arithmetic operators are used to perform arithmetic operations between two operands.

Comparison Operators: Comparison operators are used to comparing the value of the two operands and returns Boolean true or false accordingly.

Assignment Operators: The assignment operators are used to assign the value of the right expression to the left operand .

Logical Operators: The logical operators are used primarily in the expression evaluation to make a decision.

Bitwise Operators: The bitwise operators perform bit by bit operation on the values of the two operands.

Membership Operators: Python membership operators are used to check the membership of value inside a Python data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.

Identity Operators: The identity operators are used to decide whether an element certain class or type.