CH 5 Getting Started With Python Part 2
CH 5 Getting Started With Python Part 2
Precedence of Operators
• In Python, operators have different levels of precedence, which determine the order in
which they are evaluated.
• Tuples: Tuples are used when we do not need any change in the data. For example,
names of months in a year.
• Dictionary :If our data is being constantly modified or we need a fast lookup based
on a custom key ,A mobile phone book is a good application of dictionary.
EXPRESSIONS
• An expression is defined as a combination of constants, variables, and operators.
Types of operators
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Identity Operators
Membership Operators
Arithmetic Operators
Arithmetic operators that are used to perform the four basic arithmetic operations as well as
modular division, floor division and exponentiation.
Example
a=7
b=2
str1= "Hello"
str2= "Sjpuc"
# addition
# subtraction
# multiplication
# division
# floor division
# modulo
# a to the power b
Relational Operators
• Relational operator compares the values of the operands on its either side and
determines the relationship among them
example
a=5
b=2
str1="Happy"
str2="DAY"
# equal to operator
print('a == b =', a == b)
print("Is str1 equal to str2:", str1==str2 ,"\n")
print('a != b =', a != b)
Assignment Operators
• Assignment operator assigns or changes the value of the variable on its left.
Examples
# assign 10 to num1
num1 = 10
# assign 5 to num2
num2 = 5
print(num1)
Logical Operators
• There are three logical operators supported by Python. These operators (and, or, not)
• The logical operator evaluates to either True or False based on the logical operands on
either side.
Example
• x=5
o/p:True
• x=5
# returns True because one of the conditions are true (5 is greater than 3, but 5 is not
less than 4)
o/p: True
• x=5
o/p: False
Identity Operators
• Is and is not are used to check if two values are located at the same memory location
• variables are referring to the same object or not. There are two identity operators
Example
Num1=5
Type(num1) is int
o/p True
Num 1 =5
num2 = 20
o/p True
Membership Operators
Membership operators are used to check if a value is a member of the given sequence or
not
(in , not in )
Example
Input() function
• we have the input() function for taking values entered by input device such as a
keyboard.
• It accepts all user input (whether alphabets, numbers or special character) as string.
• Prompt is the string we may like to display on the screen prior to taking the input, but
it is optional.
• .The input() takes exactly what is typed from the keyboard, converts it into a string
Example
datatype ( ) function
• We can change the datatype of the string data accepted from user to an appropriate
numeric value.
• For example, the int() function will convert the accepted string to an integer
Print(type(age)
Output function()
Print( )
Python uses the print() function to output data to standard output device — the
screen.
The function print() evaluates the expression before displaying it on the screen.
The syntax for print() is: print(value [, ..., sep = ' ', end = '\n'])
• sep='separator' (optional): The objects are separated by sep. The default value of
sep is ' '.
ex:
STATEMENT
In Python, a statement is a unit of code that the Python interpreter can execute
Tokens in Python / lexical units
• Tokens or lexical units are the smallest units in the python program.
Keywords
Identifiers
Literals
String literals
Numeric literal
Boolean literal
literal collections
Operators
Punctuators
Python Constants
Explicit conversion
Explicit conversion, also called type casting
Explicitly (forced)
programmer specifies for the interpreter to convert a data type to another type
Risk of loss of information since we are forcing an expression to be of a specific
type
int(x) , str(x) ,float(x), chr(x)
General format: (new_data_type) (expression)
Example:
priceIcecream = 25
priceBrownie = 45
Output:
Implicit Conversion
data type conversion is done automatically by Python and is not instructed by the
programmer.
Implicit conversion also known as coercion
converting data into a wider-sized data type without any loss of information.
DEBUGGING
• The process of identifying and removing such mistakes, also known as bugs or errors,
from a program is called debugging.
• Errors occurring in programs can be categorised as:
i) Syntax errors
ii) Logical errors
iii) Runtime errors
i) Syntax errors
• Syntax is a rules of a language
• If any syntax error is present, the interpreter shows error message(s) and stops the
execution there.
• Ex : parentheses must be in pairs
9+(3-1 error occurs
9+(3-1) so the expression is syntactically correct.
-------------END--------------