0% found this document useful (0 votes)
48 views17 pages

Pyhton - Brushup Classes

a workshop on pyhton

Uploaded by

ssamatha8811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views17 pages

Pyhton - Brushup Classes

a workshop on pyhton

Uploaded by

ssamatha8811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

1 ) keyword:

------------------------------------
------------------------------------

it specifed the predefined meaning in programing language


..there are toatal of 35 keywords in pyhton 3.11
they are
..all keywords are in small letters except 3 they are they are in camle case they
are True ,False ,None

code:
---------
import keyword
print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',


'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

BLOCK DIAGRAM:
------------------

user-->application-->shell-->kernal-->hardware-->kernal-->shell---->application---
>user--->

if we write code in app it is called normal code if we write code in shell it is


calles script

3 ) int
-------------------
-------------------
int()
it will return default of int()
int is an individual data type in python where we can store individula values

4)float()
------------
------------
same with float

5)bool()
-------------
-------------
it is individual data type
the boole function give default boolean type as resuly

6)complex()
-------------
-------------
individual datatype
two values
(a+bj)
a is real and b as imajinary number
default value is 0j
code:
-----
complex(0)
0j
complex(1,0)
(1+0j)
complex(0,1)
1j
complex(1,1)
(1+1j)

7)TYPE CASTING
------------------
--------------------
Converting one data type to other is called type casting
--int(float(bool()))
0
--int(float(bool(float( ))))
0
--bool(-1)
True

--bool datatype dosent know positive or negative


--it knows only 0 and 1
--0 is known as flase
--values other than 0 is known as true

--float(bool(int(float(bool(-(-1))))))
1.0

8) GROUPED DATATYPES:
---------------------------
---------------------------
The datatypes whivh can store more than one value is called
8.1) STRING
-------------
Colection of character is called string
string supports indexing
index is also known as sub addressin string it supports positve index and negative
index
positive index starts from (0 to n-1)
negative(-1 to -n)
while moving left to right(0 to n-1)
R TO L (-1 to -n)
in string ' ," , """,''' ,""" give the same meaning
in string space also occupy memory

a="i am programmer"
print(len(a))
15
a[0]
'i'
a[1]
' '
print(a[15:-1])

a[-1:15]
'r'
a[-1:14]
''
a[::-1]
'remmargorp ma i'
a[-1:13]
''

9) SLICING:
---------------
it is a process of extracting the particular part of a string is called slicing
slicing have three parameters
1 start 2: stop 3: step values
a="i am programmer"
a[-1:-15:-1]
'remmargorp ma '
---a[11:15]
'mmer'
--a[-1:-5:-1]
'remm'

a[10:15]
'ammer'
a[11:15]
'mmer'
a[0:4]
'i am'
a[-1:-4:-1]
'rem'
a[-1:-5:-1]
'remm'
a[::-4]
'rara'
'rara'
'rara'
a[6:10]
'rogr'
a[1:5]
' am '
a[5:9]
'prog'
a[-11:-15]
''
a[-11:-15:-1]
' ma '

--->string is both homo and hetrogenioues

STING OBSERVATIONS
---------------------------
a=""+2
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
a=""+2
TypeError: can only concatenate str (not "int") to str
a="sai"
a*3
'saisaisai' # it will give the valuses
b=" sam"
a+b
'sai sam' # concatination we can add string with string
a*b # we cant mul string with string

Traceback (most recent call last):


File "<pyshell#49>", line 1, in <module>
a*b
TypeError: can't multiply sequence by non-int of type 'str'

-----
a="sam"
a[0]="sa"
Traceback (most recent call last):
File "<pyshell#52>", line 1, in <module>
a[0]="sa"
TypeError: 'str' object does not support item assignment
a="samatha"
print(a)
samatha

---- we cant subtract string with string

9) LIST
---------------
---------------
list is the collaction of homogenioues and hetrogenioues data types
list also dupports index as string
list suppostrs + and - index
+ve ---- 0 to n-1
-ve ----1 to -n
item assignment is allowed in list where as item assignment is not allowed in
string

syntax to creat list


--------------------
a=list()
type(a)
<class 'list'>

b=[1,2,3,"string",5,6,True,[1,2,3,4],6,7]
b[7}
SyntaxError: closing parenthesis '}' does not match opening parenthesis '['
b[7]
[1, 2, 3, 4]
b[3][0]
's'
b[7][1]
2
b[3][-len(a[3])]
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
b[3][-len(a[3])]
TypeError: object of type 'int' has no len()
b[3][-len(b[3])]
's'
list supports itam assignment
string dosent support iteam assignment

8.3) TUPLE
------------------------
------------------------

fibonaci
magic
string reverse
pi number
tuple is the homogenioue and hetrogenoues data types collection
immutable

syntax:
-------------
a=()
if we store more than one value in a variable it is known as tuple
a=()
type(a)
<class 'tuple'>
a=tuple
type(a)
<class 'type'>
a=1,2,4,4
type(a)
<class 'tuple'>
a[0]
1
a[0]=23
Traceback (most recent call last):
File "<pyshell#75>", line 1, in <module>
a[0]=23
TypeError: 'tuple' object does not support item assignment

-- tuple dont support iteam assignment

8.4)SET
---------------------
--------------------------
it is a collection of non repeated homo and hetro genioues data types
setdosent suppert indexing
order is not preserved data is stored in random order
where as in list it stored as we say

a=set()

a=set()
type(a)
<class 'set'>
a={}
type(a)
<class 'dict'>

a={1,2,3,4}
a[1]
Traceback (most recent call last):
File "<pyshell#83>", line 1, in <module>
a[1]
TypeError: 'set' object is not subscriptable

we can modify the values in the set

8.5) DICTIONARY
--------------------
----------------------
it is the collection of key value pairs
the keys must not be repeated(unique)
but the values can be repeted
syntax:
---------
a={}
a=dict()
to give the elements we write a={a:value}
to update the values
a(key)=value
a={"a":1,"b":2,"c":3,"c":5,"j":56}
a
{'a': 1, 'b': 2, 'c': 5, 'j': 56}
if key values are repeated it will take the last updted key value

note---> []---list

()---tuple
{}---dict

GROUPED DATA
---------------
string,list,tuple }--- support indexing
set,dictionary dosnet support indexing

9) OPERATORS:
------------------
------------------
total 7 types they are:

Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Bitwise Operators
Identity Operators
Membership Operators

Arithmetic operators
-------------------------
are used to perform mathematical operations like addition, subtraction,
multiplication, division, etc.
Examples:
+ (addition), - (subtraction), * (multiplication), / (division), % (modulo), //
(floor division), ** (exponentiation).
Assignment Operators:

Assignment operators
------------------------
are used to assign values to variables.
Examples:
= (assign), += (add and assign), -= (subtract and assign), *= (multiply and
assign), /= (divide and assign), etc.
Comparison Operators:

Comparison operators or relational operators


---------------------------
are used to compare two values or expressions.
They return a Boolean value (True or False) based on the comparison result.
Examples:
== (equal to), != (not equal to), < (less than), > (greater than), <= (less than
or equal to), >= (greater than or equal to).
Logical Operators:

Logical operators
------------------------
are used to combine conditional statements.
They perform logical operations such as AND, OR, and NOT.
Examples:
and (logical AND), or (logical OR), not (logical NOT).
5 and 6
6
9999 and 9
9
0 and 1
0
0 and 0
0

MMS--Memory management system


------------------------------------
if we have smae data in more than one varible it will not create more than one it
will not create morethan one memory loacation
it will share the memore adress hence and b are pointing to the smae memory
locstion
this process saving the memory is calles MMS
1 and 23
23
1or 4
1
4 or 4
4
5 or 6
5
5 and 8 and 9 or 7 or 0
9

--->print(id(a),id(b))
140715522575064 14071

Bitwise Operators:
------------------
----------------------
are used to perform bitwise operations on binary representations of numbers.
They manipulate individual bits of an operand.
Examples:
& (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left
shift), >> (right shift).
bit wise AND
--------------------
9&8
8
7&5
5
8&6
0
4&3
0
4&2
0
3&7
3
9&9
9
1&9
1
12&15
12
7&15
7
BITWISE OR
------------------
6|7|5
7
6|7
7

bitwise XOR
----------------
6^7
1
2^8
10

right shift and leftshift:


----------------------------------
40>>2
10
40<<2
160
50<<5>>2
400
5>>3
0
6<<1>>1
6

bitwise NOT:
------------------
~5
-6
~4
-5
~-2
1
~0
-1
~-1
0
~-0
-1

~43
-44
~-6
5
it will change the sign and subtract -1 from it

Identity Operators:

Identity operators
-------------------
it is used to check whether the both are pointing to the same memory or not

are used to compare the memory locations of two objects.


They determine whether two variables reference the same object.
Examples:
is (returns True if both variables point to the same object), is not (returns True
if both variables point to different objects).

a=10
b=20
a is b
False
b=10
a is b
True
c=[10]
a is b is c
False

Membership operators
-----------------------------
a="hello"
H in a
Traceback (most recent call last):
File "<pyshell#141>", line 1, in <module>
H in a
NameError: name 'H' is not defined
"H" in a
False
"h" in a
True
"sa" not in a
True
a=10
b=20
a is b
False
b=10
a is b
True
c=[10]
a is b is c
False

are used to test whether a value or variable is found within a sequence (like
lists, tuples, strings, etc.).
Examples:
in (returns True if the specified value is found in the sequence), not in (returns
True if the specified value is not found in the sequence).
These are the seven different types of operators in Python, each serving a specific
purpose in programming and allowing you to perform various operations on data.

CONTROL STATEMENTS:
---------------------------------
used to control the flow of the statements
two types ---> decitional
looping statements

a,b,c=[int(x) for x in input().split()]


if a>b and a>c:

if (b>c):
print(a,b,c,sep="\n")
else:
print(a,c,b,sep="\n")
if b>a and b>c:

if (a>c):
print(b,a,c,sep="\n")
else:
print(b,c,a,sep="\n")
if c>b and c>a:

if (b>a):
print(c,b,a,sep="\n")
else:
print(c,a,b,sep="\n")

input():
------------
it is a method which is used to take input from the user
default value is string

LOOPS:
-----------------
FOR LOOP:
====================
we dont have for loop in pyhton
here for loop is called for each loop
. to execute for loop we have to magic mathods or functions known as iter and next
iter()
-------------
iter is a method which is used to generate iterator object
. iter accepts two parameters known as callable,sentinal

next()
--------------
next is a methos which is used to take input as iterator object and it will fetch
one object at a time
if there are no elements in the object then it will rise an exception called stop
iteration exceptionml

1)even num 200 and 2000


2)11 table
4) print num from 9 to -9

3) write a program to print the fibonaci series


4) write a program to find sum of all digits as input
5) write aprogram to find the largest digit in the given number

27-03-2024

sanfoundray
interview bit
cs50

SPECIAL NUMBER:
=====================
a number whose addition of all numbers + mul of all numbers is equal to the
original number is called special number

code:
------------
n=int(input("enter the number"))
x=n
sum_=0
product=1
while(n>0):
num=n%10
sum_+=num
product*=num
n=n//10
print( "yes" if sum_+product==x else "no")

output:
------------
enter the number49
yes

3)magic number
4)abundant number
5)strong number
6) dissarium number
-----------------------
a number is said to be disarium number when the sum of its digts rised to its
position is eqal to number its self

code:
------
n = int(input("Enter the number: "))
org = n
length = 0
sum_ = 0
temp = n
while temp > 0:
temp = temp // 10
length += 1

while n > 0:
digit = n % 10
sum_ += digit ** length
n = n // 10
length -= 1
if sum_ == org:
print("Yes, it's a Dissarium number.")
else:
print("No, it's not a Dissarium number.")

output:
------------
Enter the number: 135Enter the number: 135
Yes

7)spy number:
--------------------
a number is said to be spy number if the sum of the digits is eqal to its digits
product

code:
------------
n = int(input("Enter the number: "))
org = n
sum_=0
product=1
while(n>0):
d=n%10
sum_+=d
product*=d
n=n//10
if sum_==product:
print("spy")
else:
print("no")
output:
-------------
Enter the number: 231
spy

8)spy numbers between 1 to 1000

----------------------------------
code:
----------

9) largest and the smallest spy number

PATTRENS:
----------------
1
===
n=11
for i in range(n):
for j in range(n):
if (j==0 or i==0 or i+j==n-1 or i==n-1 or j==n-1 or i==j):
print("#",end=" " )
else:
print(" ",end=" ")
print()

# # # # # # # # # # #
# # # #
# # # #
# # # #
# # # #
# # #
# # # #
# # # #
# # # #
# # # #
# # # # # # # # # # #

2)
===
n=11
for i in range(n):
for j in range(n):
if (j==0 or i==0 or i+j==n-1 or i==n-1 or j==n-1 or i==j or i==n//2 or
j==n//2):
print("#",end=" " )
else:
print(" ",end=" ")
print()

output:
-----------
# # # # # # # # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # # # # # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # # # # # # # #

3)
==
n=11
for i in range(n):
for j in range(n):
if (i==j or i+j==n-1):
print("#",end=" ")
else:
print(" ",end=" ")
print()

output:
-------
# #
# #
# #
# #
# #
#
# #
# #
# #
# #
# #

4)
n=11
a=1
b=6
for i in range(n):
for j in range(n):
if (i==j):
print(a,end=" ")
a+=1
elif(i+j==n-1):

print(b,end=" ")
b+=1
else:
print(" ",end=" ")
print()
output:
========
1 6
2 7
3 8
4 9
5 10
6
11 7
12 8
13 9
14 10
15 11

n=11
for i in range(n):
for j in range(n):
if (i==0 and j==0 or i==0 and j==n-1 or i==n-1 and j==0 or i==n-1 and j==n-
1 or i==j and i+j==n-1):
print("#",end=" ")

else:
print(" ",end=" ")
print()

output:
===========

# #

# #

n=11
for i in range(n):
for j in range(n):
if i-j>=0:
print(i,end=" ")
else:
print(" ",end=" ")
print()
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6 6
7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10 10

code:
---------
n=11
for i in range(n):
for j in range(n):
if i-j>=0:
print(j,end=" ")
else:
print(" ",end=" ")
print()

output:
---------
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10

You might also like