KEMBAR78
PYTHON PROGRAMMING PROBLEM SOLVING PPT DOCUMENTS PPT | PPTX
Unit 1
Part C : Basics of Python
Programming
By Prof. S.S.Wadnere
1. Features of Python,
2. History and Future of Python
3. Writing and executing Python
4. Program Literal constants, variables and identifiers,
Data Types, Input operation, Comments, Reserved
words, Indentation, Operators and expressions,
Expressions in
Python. SNJB KBJ COE, Chandwad
History of
Python
1. Python is an interpreted, high-level, general-
purpose programming language.
2. Created by Guido van Rossum and first released in
1991
3. Python 2.0, released in 2000
4. The Python 2 language, i.e. Python 2.7.x, was
officially discontinued on January 1, 2020
5. A global community of programmers develops
and maintains python.
6. Python 3.0 was released on 3 December 2008. It was
a major revision of the language that is not
completely backward-compatible.
Guido van
Rossum at
OSCON 2006
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
•Data Science.
•Machine Learning.
•Web Development.
•Computer Science Education.
•Game Development.
•Medicine and Pharmacology.
•Biology and Bioinformatics.
•Computer Vision and Image Processing.
Python in Real-World Scenarios
Python is used in virtually every industry and scientific field that you can imagine,
including:
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
• Neuroscience and Psychology.
• Astronomy.
• Other areas such as robotics,
autonomous vehicles, business,
meteorology, and graphical user interface
(GUI) development.
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Python IDLE (Integrated Development and Learning Environment)
Shell
Interactive
Mode
Script
Mode
Writing Python Programs
Step 1: Open an editor
Step 2: Write the
instructions (Program
Code)
Step 3: Save it as a file
Literals Constants
• The value of literal constant can used directly in program.
• It is a constant because its value cannot be changed.
• Ex. 7, 8.4, ‘A’ and “Hello”
Numbers
• Number as name suggests, refer to a numeric value.
• Four type of number used in python program.
By Prof. S.S.Wadnere
• Integers
• Long Integers
• Floating Point
• Complex number
2380
467838675843L
23.57 or 91.5E-2
numbers of a+bi form (Like -3 + 7i)
SNJB KBJ COE, Chandwad
String Literal
• String is group of characters, also known as text.
• We can use string using
• Single Quotes (‘)
• Double Quotes (“)
• Triple Quotes (“’)
• All spaces and tabs within string are preserved in quotes.
String literal concatenation
•Python concatenates two string that places side by
side Raw String
• String without any escape sequence, display exactly as
spacified.
Unicode
• Unicode is standard way of writing international text.
By Prof. S.S.Wadnere
Boolean Literal
• Boolean data type variable can have one of the two value – True or
False.
• When you compare two values, the expression is evaluated and
Python returns the Boolean answer.
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Ex.
print(10 > 9)
print(10 == 9)
print(10 < 9)
Output: True
Output: False
Output: False
Variable
• Variable are reserved memory location that store value.
• Every variable assigned a name which can be used to refer to the
value later in the program.
• Variable can hold value of different types called data types.
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Identifiers
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
• Identifiers Known as name given to identity something.
• This something can be variable, function, class, module or other user
defined objects.
• For naming there are some basic rules,
• First character must be underscore _ or letter( alphabets ).
• Rest of the identifier name can be _ , letter or numbers.
• Identifier names are case-sensitive
• Any other symbols are not allowed with in identifiers
•
Assigning or Initializing Value to Variables
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
• In python programmers need not explicitly declare variable to reserve
memory space.
• The declaration done automatically when a value assigned to the
variable using the = operator.
• You can reassign variables as many times as you want to change the
value stored in them.
•Variable can be deleted by using del
statement. Multiple Assignment
• Python allow single value to multiple variables
• Ex. sum = flag = a = b = 0
• Ex. sum , a , b , mesg = 0 ,39.45 , 52, ”Result”
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Print Function
• The print() function prints the specified message to the screen, or
other standard output device.
• The message can be a string, or any other object, the object will be
converted into a string before written to the screen.
Example :
print("Hello World")
x = ("apple", "banana", "cherry")
print(x)
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Python 3.6 uses the input() method.
Python 2.7 uses the raw_input()
method.
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Input Function
• Input method can use to take input from users.
• Input function always take user input in the form of string only.
• User can convert the data type using casting functions.
Example:
a = input(“Enter the number: ”)
B = int(input(“Enter the number: ”))
 Write a python program to addition of two numbers using input function
Built-in format()function
• The format () function produces a numeric string of a floating point
value rounded to a specific number of decimal places.
# Without using format()
>>>16/3
5.333333333333333
# Using format()
>>> format(16/3,'.2f')
'5.33'
By Prof. S.S.Wadnere
• Simple Operation of number
• Division by zero
• Dividing two integers
• Quotient and Remainder
SNJB KBJ COE, Chandwad
Type Conversion
By Prof. S.S.Wadnere
• In Python certain operations that involves different types of data. To perform
this operation data type conversion is required.
Function Description
int(y) It converts y to an integer.
float(y) It converts y to a floating-point number.
long(y) It converts y to an float.
str(y) It converts y to a string.
tuple(y) It converts y to a tuple.
list(y) It converts y to a list.
set(y) It converts y to a set.
dict(y) It creates a dictionary and y should be a sequence of (key,value) tuples.
ord(y) It converts a character into an integer.
hex(y) It converts an integer to a hexadecimal string.
oct(y) It converts an inteSgNeJBrKtBoJ COaEn, Choacndtwaaldstring
Escape Sequences
• Some characters cannot be directly included in a string.
• Such character must be escaped by placing a backslash before them.
By Prof. S.S.Wadnere
Escape
Sequence Description Example Output
 Prints Backslash print "" 
` Prints single-quote print "'" '
" Prints double quote print """ "
a Ringing the bell alert sounds ( eg. xterm ) print "a" N/A
b Removes previous character print "ab" + "b" + "c" ac
n ASCII linefeed ( LF ) print "hellonworld"
hello
world
t ASCII horizontal tab (TAB). PrinStNsJBTKABJBCOE,
Chandwad
print "t* hello" * hello
Comments
By Prof. S.S.Wadnere
• Comments are non-executable statements in program.
• They are just added to describe the statements in the program code.
• All characters following by # consider as comments.
•A program can have any number of
comments. Ex.
#This is a comment
print("Hello, World!")
Multi line Comments
Ex.
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
SNJB KBJ COE, Chandwad
Reserved Words
• This word are which have pre-defined meaning.
• We can not be use this reserved word for naming of identifiers.
and assert in
del else raise
from if continue
not pass finally
while yield is
as break return
elif except def
global import for
or print lambda
with SNJBcKlBaJsCsOE,
Chand
wad try
e
Indentation
• Whitespaces and tabs that are used at the beginning of a statement.
• The statements with the same indentation belong to the same group
called a suite.
• Statements in a block must have same indentation level.
•
if a==1:
print(a)
if b==2:
print(b)
print('end')
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Operators and Expressions
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
• Operators are the constructs that are used to manipulate the value of
operands.
• Python support different types of operators
• Arithmetic operators
• Comparison (Relation) operators
• Assignment operators
• Logical operators
• Unary operators
• Bitwise operators
• Membership operators
• Identity operators
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
• Arithmetic Operators
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B / A = 2
% Modulus Operator and remainder of after an
integer division.
B % A = 0
// Floor Division: Divides the operands and
returns the quotient.
12//5 = 2
** Exponent: Perform exponential calculation. 3**2 = 9
By Prof. S.S.Wadnere
• Comparison Operators : Used to perform comparison. Relational operators
always results in Boolean value either 1 TRUE or 0 FALSE. Following is the list
Operator Description Example (A=20 B=50)
== Checks if the values of two operands are equal or
not.
(A == B) is not true.
!= Checks if the values of two operands are equal or
not.
(A != B) is true.
> Checks if the value of left operand is greater than the
value of right operand.
(A > B) is not true.
< Checks if the value of left operand is less than the
value of right operand
(A < B) is true.
>= Checks if the value of left operand is greater than or
equal to the value of right operand.
(A >= B) is not true.
<= Checks if the value of left operand is less than or
equal to the value of right operand. If yes, then the
condition becomes true.
SNJB KBJ COE, Chandwad
(A <= B) is true.
By Prof. S.S.Wadnere
• Assignment Operators
Operator Description Example
= Simple assignment operator C = A + B will assign the value
of A + B to C
+= Add AND assignment operator. C += A is equivalent to C = C +
A
-= Subtract AND assignment operator. C -= A is equivalent to C = C -
A
*= Multiply AND assignment operator. C *= A is equivalent to C = C *
A
/= Divide AND assignment operator. C /= A is equivalent to C = C /
A
%= Modulus AND assignment operator. C %= A is equivalent to C = C
% A
SNJB KBJ COE, Chandwad
• Bitwise Operators : are used to perform binary arithmetic.
Operator Use Example
~ Not ~a
& AND a&b
| OR a|b
^ Xor a^b
<< Shift left a<<2
>> Shift Right b>>2
p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 JB KBJ CO1E,
Chandw
ad 1
By Prof. S.S.Wadnere
By Prof. S.S.Wadnere
• Logical Operators :
Operator Description Example
and
Called Logical AND operator. If both the
operands are non-zero, then the condition
becomes true.
(A and B)
is false.
or
Called Logical OR Operator. If any of the two
operands is non-zero, then the condition
becomes true.
(A or B)
is true.
not
Called Logical NOT Operator. It is used to reverse
the logical state of its operand. If a condition is
true, then Logical NOT operator will make it
false.
not(A and B)
is true.
SNJB KBJ COE, Chandwad
Membership Operator
Operator Description Example
in Returns True if a sequence with the specified
value is present in the object
x in y
not in Returns True if a sequence with the specified
value is not present in the object
x not in y
This operator test for membership in a sequence such as string, list ,or tuples.
Identity Operator
By Prof. S.S.Wadnere
Operator Description Example
is Returns true if operands or values on both
sides is same
x is y
is not Returns true if operands or values on both
sides of the operator does not point to the
same object SNJB KBJ COE, Chand
x not is y
wad
Python Operators Precedence and Associativity
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Operator Description
** Exponentiation (raise to the power)
~ + - Complement, unary plus and minus (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^ | Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Expressions
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
• An expression is any combination of symbols (like variables, constants and operators).
• In Python, an expression must have at least one operand and can have one or more operators.
• On evaluating an expression, we get a value.
Based on the position of operator in an expression
Infix Expressions: The operator placed in between the operands. Example: a = b – c
Prefix Expressions: The operator placed before the operands. Example: a = - 10
Postfix Expressions: The operator placed after the operands. Example: a = 10 -
Expressions
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Based on the position of operator in an expression
Constant Expressions: Use Only constants. Example: 8 + 9 – 2
Integral Expressions: produces an integer result after evaluating the expression. Example: a = 10
Floating Point Expressions: Produces floating point results. Example: a * b / 2
Relational Expressions: Returns either true or false value. Example: c = a>b
Logical Expressions: Involve relational operator, returns True or False. Example: a>b && y! = 0
Bitwise Expressions: Manipulates data at bit level. Example: x = y&z
Assignment Expressions: Assigns a value to a variable. Example: c = a + b or c = 10
Expressions in Python
• In Python, operators are special symbols to perform some operations.
• The values that an operator acts on are called operands.
Type of Expressions
• Infix Expression : operator place between the operands. Ex. a = b
- c
• Prefix Expression : operator place before the operands. Ex. a = - b
c
• Postfix Expression : operator place after the operands. Ex. a = b c
-
Data type based expressions
• Assignment Expression : a = 10 or b = 20
• Constant Expression : Ex. 4 + 2 - 3
• Integral Expression : Ex. c = a + b
• Floating Point Expression : Ex. a * b / 2
By Prof. S.S.Wadnere
Operations on Strings
Examples:
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
• Concatenation
• Multiplication or String Repetition
• Slice a String
Slice Operations on Strings
• You can extract subsets of strings by using the
slice
operator ([ ] and [:]).
• You need to specify index or the range of index of
characters to be extracted.
• The index of the first character is 0 and the index of the
last character is n-1, where n is the number of characters
• If you want to extract characters starting from the end of
the string, then you must specify the index as a
negative number. For example, the index of the last
character is -1.
Examples:
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Examples:
SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere
Programming Examples
• Write a program to enter a number and display its HEX and Octal equivalent.
• Write a program to calculate area of triangle using Heron’s formula.
• Write a program to swap two numbers.
• Write a program that accepts an object’s mass (in kilograms) and velocity (in
meters per second) and displays its momentum.
• Write a program to calculate seconds in day.
• Write print function for following output.
'/'/'/'
'/'/'/' '/'/'/'
'/'/'/' SNJB KBJ COE, Chandwad
By Prof. S.S.Wadnere

PYTHON PROGRAMMING PROBLEM SOLVING PPT DOCUMENTS PPT

  • 1.
    Unit 1 Part C: Basics of Python Programming By Prof. S.S.Wadnere 1. Features of Python, 2. History and Future of Python 3. Writing and executing Python 4. Program Literal constants, variables and identifiers, Data Types, Input operation, Comments, Reserved words, Indentation, Operators and expressions, Expressions in Python. SNJB KBJ COE, Chandwad
  • 2.
    History of Python 1. Pythonis an interpreted, high-level, general- purpose programming language. 2. Created by Guido van Rossum and first released in 1991 3. Python 2.0, released in 2000 4. The Python 2 language, i.e. Python 2.7.x, was officially discontinued on January 1, 2020 5. A global community of programmers develops and maintains python. 6. Python 3.0 was released on 3 December 2008. It was a major revision of the language that is not completely backward-compatible. Guido van Rossum at OSCON 2006 SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere
  • 3.
    •Data Science. •Machine Learning. •WebDevelopment. •Computer Science Education. •Game Development. •Medicine and Pharmacology. •Biology and Bioinformatics. •Computer Vision and Image Processing. Python in Real-World Scenarios Python is used in virtually every industry and scientific field that you can imagine, including: SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere • Neuroscience and Psychology. • Astronomy. • Other areas such as robotics, autonomous vehicles, business, meteorology, and graphical user interface (GUI) development.
  • 4.
    SNJB KBJ COE,Chandwad By Prof. S.S.Wadnere
  • 5.
    Python IDLE (IntegratedDevelopment and Learning Environment) Shell Interactive Mode Script Mode Writing Python Programs Step 1: Open an editor Step 2: Write the instructions (Program Code) Step 3: Save it as a file
  • 6.
    Literals Constants • Thevalue of literal constant can used directly in program. • It is a constant because its value cannot be changed. • Ex. 7, 8.4, ‘A’ and “Hello” Numbers • Number as name suggests, refer to a numeric value. • Four type of number used in python program. By Prof. S.S.Wadnere • Integers • Long Integers • Floating Point • Complex number 2380 467838675843L 23.57 or 91.5E-2 numbers of a+bi form (Like -3 + 7i) SNJB KBJ COE, Chandwad
  • 7.
    String Literal • Stringis group of characters, also known as text. • We can use string using • Single Quotes (‘) • Double Quotes (“) • Triple Quotes (“’) • All spaces and tabs within string are preserved in quotes. String literal concatenation •Python concatenates two string that places side by side Raw String • String without any escape sequence, display exactly as spacified. Unicode • Unicode is standard way of writing international text. By Prof. S.S.Wadnere
  • 8.
    Boolean Literal • Booleandata type variable can have one of the two value – True or False. • When you compare two values, the expression is evaluated and Python returns the Boolean answer. SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere Ex. print(10 > 9) print(10 == 9) print(10 < 9) Output: True Output: False Output: False
  • 9.
    Variable • Variable arereserved memory location that store value. • Every variable assigned a name which can be used to refer to the value later in the program. • Variable can hold value of different types called data types. SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere
  • 10.
    Identifiers SNJB KBJ COE,Chandwad By Prof. S.S.Wadnere • Identifiers Known as name given to identity something. • This something can be variable, function, class, module or other user defined objects. • For naming there are some basic rules, • First character must be underscore _ or letter( alphabets ). • Rest of the identifier name can be _ , letter or numbers. • Identifier names are case-sensitive • Any other symbols are not allowed with in identifiers •
  • 11.
    Assigning or InitializingValue to Variables SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere • In python programmers need not explicitly declare variable to reserve memory space. • The declaration done automatically when a value assigned to the variable using the = operator. • You can reassign variables as many times as you want to change the value stored in them. •Variable can be deleted by using del statement. Multiple Assignment • Python allow single value to multiple variables • Ex. sum = flag = a = b = 0 • Ex. sum , a , b , mesg = 0 ,39.45 , 52, ”Result”
  • 12.
    SNJB KBJ COE,Chandwad By Prof. S.S.Wadnere
  • 13.
    Print Function • Theprint() function prints the specified message to the screen, or other standard output device. • The message can be a string, or any other object, the object will be converted into a string before written to the screen. Example : print("Hello World") x = ("apple", "banana", "cherry") print(x) SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere
  • 14.
    Python 3.6 usesthe input() method. Python 2.7 uses the raw_input() method. SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere Input Function • Input method can use to take input from users. • Input function always take user input in the form of string only. • User can convert the data type using casting functions. Example: a = input(“Enter the number: ”) B = int(input(“Enter the number: ”))  Write a python program to addition of two numbers using input function
  • 15.
    Built-in format()function • Theformat () function produces a numeric string of a floating point value rounded to a specific number of decimal places. # Without using format() >>>16/3 5.333333333333333 # Using format() >>> format(16/3,'.2f') '5.33' By Prof. S.S.Wadnere • Simple Operation of number • Division by zero • Dividing two integers • Quotient and Remainder SNJB KBJ COE, Chandwad
  • 16.
    Type Conversion By Prof.S.S.Wadnere • In Python certain operations that involves different types of data. To perform this operation data type conversion is required. Function Description int(y) It converts y to an integer. float(y) It converts y to a floating-point number. long(y) It converts y to an float. str(y) It converts y to a string. tuple(y) It converts y to a tuple. list(y) It converts y to a list. set(y) It converts y to a set. dict(y) It creates a dictionary and y should be a sequence of (key,value) tuples. ord(y) It converts a character into an integer. hex(y) It converts an integer to a hexadecimal string. oct(y) It converts an inteSgNeJBrKtBoJ COaEn, Choacndtwaaldstring
  • 17.
    Escape Sequences • Somecharacters cannot be directly included in a string. • Such character must be escaped by placing a backslash before them. By Prof. S.S.Wadnere Escape Sequence Description Example Output Prints Backslash print "" ` Prints single-quote print "'" ' " Prints double quote print """ " a Ringing the bell alert sounds ( eg. xterm ) print "a" N/A b Removes previous character print "ab" + "b" + "c" ac n ASCII linefeed ( LF ) print "hellonworld" hello world t ASCII horizontal tab (TAB). PrinStNsJBTKABJBCOE, Chandwad print "t* hello" * hello
  • 18.
    Comments By Prof. S.S.Wadnere •Comments are non-executable statements in program. • They are just added to describe the statements in the program code. • All characters following by # consider as comments. •A program can have any number of comments. Ex. #This is a comment print("Hello, World!") Multi line Comments Ex. """ This is a comment written in more than just one line """ print("Hello, World!") SNJB KBJ COE, Chandwad
  • 19.
    Reserved Words • Thisword are which have pre-defined meaning. • We can not be use this reserved word for naming of identifiers. and assert in del else raise from if continue not pass finally while yield is as break return elif except def global import for or print lambda with SNJBcKlBaJsCsOE, Chand wad try e
  • 20.
    Indentation • Whitespaces andtabs that are used at the beginning of a statement. • The statements with the same indentation belong to the same group called a suite. • Statements in a block must have same indentation level. • if a==1: print(a) if b==2: print(b) print('end') SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere
  • 21.
    Operators and Expressions SNJBKBJ COE, Chandwad By Prof. S.S.Wadnere • Operators are the constructs that are used to manipulate the value of operands. • Python support different types of operators • Arithmetic operators • Comparison (Relation) operators • Assignment operators • Logical operators • Unary operators • Bitwise operators • Membership operators • Identity operators
  • 22.
    SNJB KBJ COE,Chandwad By Prof. S.S.Wadnere • Arithmetic Operators Operator Description Example + Adds two operands. A + B = 30 − Subtracts second operand from the first. A − B = -10 * Multiplies both operands. A * B = 200 / Divides numerator by de-numerator. B / A = 2 % Modulus Operator and remainder of after an integer division. B % A = 0 // Floor Division: Divides the operands and returns the quotient. 12//5 = 2 ** Exponent: Perform exponential calculation. 3**2 = 9
  • 23.
    By Prof. S.S.Wadnere •Comparison Operators : Used to perform comparison. Relational operators always results in Boolean value either 1 TRUE or 0 FALSE. Following is the list Operator Description Example (A=20 B=50) == Checks if the values of two operands are equal or not. (A == B) is not true. != Checks if the values of two operands are equal or not. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. SNJB KBJ COE, Chandwad (A <= B) is true.
  • 24.
    By Prof. S.S.Wadnere •Assignment Operators Operator Description Example = Simple assignment operator C = A + B will assign the value of A + B to C += Add AND assignment operator. C += A is equivalent to C = C + A -= Subtract AND assignment operator. C -= A is equivalent to C = C - A *= Multiply AND assignment operator. C *= A is equivalent to C = C * A /= Divide AND assignment operator. C /= A is equivalent to C = C / A %= Modulus AND assignment operator. C %= A is equivalent to C = C % A SNJB KBJ COE, Chandwad
  • 25.
    • Bitwise Operators: are used to perform binary arithmetic. Operator Use Example ~ Not ~a & AND a&b | OR a|b ^ Xor a^b << Shift left a<<2 >> Shift Right b>>2 p q p & q p | q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 JB KBJ CO1E, Chandw ad 1 By Prof. S.S.Wadnere
  • 26.
    By Prof. S.S.Wadnere •Logical Operators : Operator Description Example and Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A and B) is false. or Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A or B) is true. not Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. not(A and B) is true. SNJB KBJ COE, Chandwad
  • 27.
    Membership Operator Operator DescriptionExample in Returns True if a sequence with the specified value is present in the object x in y not in Returns True if a sequence with the specified value is not present in the object x not in y This operator test for membership in a sequence such as string, list ,or tuples. Identity Operator By Prof. S.S.Wadnere Operator Description Example is Returns true if operands or values on both sides is same x is y is not Returns true if operands or values on both sides of the operator does not point to the same object SNJB KBJ COE, Chand x not is y wad
  • 28.
    Python Operators Precedenceand Associativity SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere Operator Description ** Exponentiation (raise to the power) ~ + - Complement, unary plus and minus (method names for the last two are +@ and -@) * / % // Multiply, divide, modulo and floor division + - Addition and subtraction >> << Right and left bitwise shift & Bitwise 'AND' ^ | Bitwise exclusive `OR' and regular `OR' <= < > >= Comparison operators <> == != Equality operators = %= /= //= -= += *= **= Assignment operators is is not Identity operators in not in Membership operators not or and Logical operators
  • 29.
    Expressions SNJB KBJ COE,Chandwad By Prof. S.S.Wadnere • An expression is any combination of symbols (like variables, constants and operators). • In Python, an expression must have at least one operand and can have one or more operators. • On evaluating an expression, we get a value. Based on the position of operator in an expression Infix Expressions: The operator placed in between the operands. Example: a = b – c Prefix Expressions: The operator placed before the operands. Example: a = - 10 Postfix Expressions: The operator placed after the operands. Example: a = 10 -
  • 30.
    Expressions SNJB KBJ COE,Chandwad By Prof. S.S.Wadnere Based on the position of operator in an expression Constant Expressions: Use Only constants. Example: 8 + 9 – 2 Integral Expressions: produces an integer result after evaluating the expression. Example: a = 10 Floating Point Expressions: Produces floating point results. Example: a * b / 2 Relational Expressions: Returns either true or false value. Example: c = a>b Logical Expressions: Involve relational operator, returns True or False. Example: a>b && y! = 0 Bitwise Expressions: Manipulates data at bit level. Example: x = y&z Assignment Expressions: Assigns a value to a variable. Example: c = a + b or c = 10
  • 31.
    Expressions in Python •In Python, operators are special symbols to perform some operations. • The values that an operator acts on are called operands. Type of Expressions • Infix Expression : operator place between the operands. Ex. a = b - c • Prefix Expression : operator place before the operands. Ex. a = - b c • Postfix Expression : operator place after the operands. Ex. a = b c - Data type based expressions • Assignment Expression : a = 10 or b = 20 • Constant Expression : Ex. 4 + 2 - 3 • Integral Expression : Ex. c = a + b • Floating Point Expression : Ex. a * b / 2 By Prof. S.S.Wadnere
  • 32.
    Operations on Strings Examples: SNJBKBJ COE, Chandwad By Prof. S.S.Wadnere • Concatenation • Multiplication or String Repetition • Slice a String
  • 33.
    Slice Operations onStrings • You can extract subsets of strings by using the slice operator ([ ] and [:]). • You need to specify index or the range of index of characters to be extracted. • The index of the first character is 0 and the index of the last character is n-1, where n is the number of characters • If you want to extract characters starting from the end of the string, then you must specify the index as a negative number. For example, the index of the last character is -1. Examples: SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere
  • 34.
    Examples: SNJB KBJ COE,Chandwad By Prof. S.S.Wadnere
  • 35.
    Programming Examples • Writea program to enter a number and display its HEX and Octal equivalent. • Write a program to calculate area of triangle using Heron’s formula. • Write a program to swap two numbers. • Write a program that accepts an object’s mass (in kilograms) and velocity (in meters per second) and displays its momentum. • Write a program to calculate seconds in day. • Write print function for following output. '/'/'/' '/'/'/' '/'/'/' '/'/'/' SNJB KBJ COE, Chandwad By Prof. S.S.Wadnere