Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
smhuda
GitHub Repository: smhuda/python-pcep-notes
Path: blob/main/Module 3.ipynb
77 views
Kernel: Python 3

Operators

  • Binary

  • Unary

  • Priorities

  • Binding

Bitwise

  • Bitwise not ~

  • And &

  • Xor ^

  • Shift left <<

  • Shift right >>

~2
-3
2 & 3
2
1 ^ 4
5

Boolean Operation

  • And

  • OR

x = 7 y = 3 x > y and (x-y) < y
False
x = 7 y = 3 x > y or y > x
True

Relational operators

  • Equals =

  • Not equals !=

  • Greater than >

  • Greater or equal >=

  • Less than <

  • Less or equal <=

'Dublin' == 'dublin'
False
5 > 2
True
4 <= 8
True

IO Operations

  • input()

  • print()

  • int()

  • float()

  • str()

  • len()

type(int("25"))
int
type(float('6.54'))
float
len('Dublin')
6

Type Casting (Conversion of Data Stream to another type)

x = 1 y = 5 my_string = "{} + {} = {}".format(x,y,x+y) my_string
'1 + 5 = 6'

Conditional Operators

  • If

  • If-else

  • If-elif

  • If-elif-else

x = 2 y = 4 if x > y: print("X is of larger value") else: print("X is not larger in value than Y")
X is not larger in value than Y
x = 2 if x == 2: pass
x = 3 y = 4 if x > 5: print("X is not 0") elif y == 2: print("Value of Y is 2") else: print("X is not 1 and Y is 2")
X is not 1 and Y is 2

Loops

  • For

  • While

for i in range(5): print(i)
0 1 2 3 4
i = 1 while True: print(i) if i == 10: break else: i = i + 1

Sequences

  • Set

  • Tuple

  • List

  • Dict

a = set((1,2,3,4,5)) for i in a: print(i)
1 2 3 4 5
a = list([1,2,3,4,5]) for i in a: print(i)
1 2 3 4 5
for i in range(10): print(i) if i == 8: break else: print("Didnt finish gracefully")
0 1 2 3 4 5 6 7 8
for i in range(10): if i % 2 == 0: continue print("This number {} is odd!".format(i))
This number 1 is odd! This number 3 is odd! This number 5 is odd! This number 7 is odd! This number 9 is odd!
# break - example print("The break instruction:") for i in range(1, 6): if i == 3: break print("Inside the loop.", i) print("Outside the loop.") # continue - example print("\nThe continue instruction:") for i in range(1, 6): if i == 3: continue print("Inside the loop.", i) print("Outside the loop.")
The break instruction: Inside the loop. 1 Inside the loop. 2 Outside the loop. The continue instruction: Inside the loop. 1 Inside the loop. 2 Inside the loop. 4 Inside the loop. 5 Outside the loop.
i = 1 while i < 5: print(i) i += 1 else: print("else:", i)
1 2 3 4 else: 5
# Example 1 word = "Python" for letter in word: print(letter, end="*") # Example 2 for i in range(1, 10): if i % 2 == 0: print(i)
P*y*t*h*o*n*2 4 6 8
text = "pyxpyxpyx" for letter in text: if letter == "x": continue print(letter, end="")
pypypy