Day 3 Python Operators

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Python Fundamentals

ARITHMETIC OPERATORS
Python Basic Arithmetic Operators
x = 7, y = 2

Addition: x + y = 9
Subtraction: x - y = 5
Multiplication: x*y = 14
Division: x/y = 3.5

Floor Division: x//y = 3 (rounds to the nearest)


Modulus: x%y = 1 (gives the remainder when divide)
Exponent: x**y = 49 (7 to the power of 2)
Numbers and Arithmetic Operators Exercise
Create a simple program to solve the
following mathematical problems

1) Assign A and B with values


2) Find the solution for (A+B)2

(A+B)2 = A2 + 2*A*B + B2
Python Fundamentals

ASSIGNMENT OPERATORS
Using Variables in Python : Assignment
x = x + 2
we can also write
x += 2

x = x - 2
we can also write
x -= 2
Python Fundamentals

COMPARISION OPERATORS
Comparison Operators
5 == 2 Equality

5 != 2 Non-Equality

5>2 Greater than

2<5 Smaller than

5>=2 Greater than or equal to

2 <= 5 Less than or equal to


Python Fundamentals

LOGICAL OPERATORS
LOGICAL Operators – and, or , not
5 == 5 and 2 < 1 will return False

5 == 5 or 2 < 1 will return True

not 2 < 1 will return True

not 2 > 1 will return False


Triple Quotes and Escape Chars
• To display a long message in multi lines, we can use the triple-quote
print (’’’Hello World.
How are you doing.’’’)

Escape chars to print "unprintable" characters

print (‘Hello\nWorld’)\n (Prints a newline)


print (‘\\’)(Prints a backslash)
print (\’)(Prints a single quotes)
print (\”) (Prints a double quotes)
Python Fundamentals

INPUT and OUTPUT


Input() and print()
• Input() statement prompts the user for input
• It always get the information as a string

studName = input("Enter student name: ")


studAge = input("Enter student age: ")

print() statement for output – three ways to print variables


print("The student name is", myName, "and is",myAge, "years old.") OR

print("The student name is %s and is %s years old." %(studName, studAge)) OR

print("The student name is {} and is {} years old.".format(studName, studAge))

You might also like