DSA in Python
· Data structures: palcing information in proper order
· Data placed in horizontal:Array
· Data placed in vertical: Stacks
· Most used algorithms: 1)searching algorithm 2)sorting algorithm
· Python by Guido Van Rossum in 1991
· C by Dennis Richei in 1972
· java by James Gosling in 1995
Python introduction:
· Data types: int, float, bool, string,complex
· semi column(;) is optional
KEYWORDS:(32)
True False None and
or not is if
else elif for while
break continue pass try
except finally raise assert
def return lamda yeild
class import from in
as del global with
nonlocal async await
Operators:
1) Arthematic operators:
priority: **(exponential)
* / % //(floor)
+ -
:+ :=(walrus operator)(assignment in if condition)
2)Assignment: =,:=
3)Relational: <,>,<=,>=, (==,!=)equality operators 0-True
4)Logical: not, and, or 1-False
5)Compound: +=,,-=,*=,/=,%=
6)Bitwise: <<(a<<b === a*(2^b))
>>(a>>b ===a/(2^b))
7)Membership: in , not in
8)Conditional statements:
· if , if else,if elif ladder
· using paranthesis in conditions is optional
3) data stuctures:
· List
· Tuple
· Set
· Dictionary
4)iterative statements:
a)while (condition):
statement 1
staement 2........
power of three program:
class Solution(object):
def isPowerOfThree(self, n):
if n<=0:
return False OR return n<=1 and 1162261467%n==0
while n%3==0:
n//=3
return n==1
b)for in range(var name)
· To print list of keywords present in python: # import keyword
# keyword.kwlist
Number Systems :
· decimal
· binary 2^n 2^0
· octal 8^n 8^0
· hexa decimal
inputs:
0b1010 binary
0o123 octal
0xFF hexa
0XF decimal
outputs : int(), bin(), oct(), hex()
character:
chr(122) === z
ord("z") === 122
number after a double reversal
class Solution(object):
def isSameAfterReversals(self, num):
"""
:type num: int
:rtype: bool
"""
orig=num
rev1 =0
rev2=0
rem=0
while num!=0:
rem=num%10
rev1 = rem+ rev1*10
num=num//10
num=rev1
while num!=0:
rem=num%10
rev2 = rem+ rev2*10
num=num//10
if(orig==rev2):
return True
else:
return False
OR