0% found this document useful (0 votes)
10 views9 pages

Pythonprograms Week1 and Week2

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

Pythonprograms Week1 and Week2

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

GONE MADHUSRI

CSD-A
25255A6704
Week- 1

ASSIGNMENT-1

1. Write a program to demonstrate different number data types in


Python.

Program:

a=2

b=3.456

c=(2.3+5.7j)

print(type(a))

print(type(b))

print(type(c))

print(c.real)

print(c.imag)

print(c.conjugate())

print(float(a))

print(complex(a))

print(int(b))

print(complex(b))

o=34

s=35.7

print(complex(o))

print(complex(s))

print(abs(a))
GONE MADHUSRI
CSD-A
25255A6704
print(abs(b))

print(abs(c))

n=9

print(abs(n))

print(divmod(45,8))

print(pow(2,20))

print(round(2.344535333))

print(oct(345))

print(hex(345))

num=102

char=chr(num)

print(char)

char='f'

num=ord(char)

print(num)

p=2.346664553676357

d=2009102.3342323524

print("floating point number p is",p)

print("floating point number d is ",d)

Output:

<class 'int'>

<class 'float'>
GONE MADHUSRI
CSD-A
25255A6704
<class 'complex'>

2.3

5.7

(2.3-5.7j)

2.0

(2+0j)

(3.456+0j)

(34+0j)

(35.7+0j)

3.456

6.146543744251724

(5, 5)

1048576

0o531

0x159

102

floating point number p is 2.346664553676357 floating point number d is


2009102.3342323
GONE MADHUSRI
CSD-A
25255A6704
2. Write a program to illustrate various types of operations in Python.

Program:
x=20
y=15
print("addition of x,y is:", x+y)
print("subtraction of x,y: " ,x-y)
print("multiplication of x,y :", x*y)
print("division of x,y :" ,x/y)
print("modulus of x,y :" ,x%y)
print("power of x,y :",pow(x,y))
c1=(6+3j)
c2=(7-4j)
print(c1+c2)
print(c1-c2)
print(c1*c2)

Output:

addition of x,y is: 35

subtraction of x,y: 5

multiplication of x,y : 300

division of x,y : 1.3333333333333333

modulus of x,y : 5

power of x,y : 32768000000000000000

(13-1j)

(-1+7j)

(54-3j)
GONE MADHUSRI
CSD-A
25255A6704

Week- 2

ASSIGNMENT-2

1. Write a Python program to find largest of three numbers

Program:

a=int(input("enter first number:"))

b=int(input("enter second number:"))

c=int(input("enter third number:"))

if a>b:

if a>c:

print("a is largest number")

else:

if b>c:

print("b is the largest number")

else:

print("c is largest number")

Output:

enter first number:467

enter second number:567

enter third number:344

b is the largest number


GONE MADHUSRI
CSD-A
25255A6704

2. Write a Python program to convert temperatures to and from Celsius,


Fahrenheit. [Formula: c/5 = f-32/9]
Program:

c=float(input("enter temperature in celcius: "))


print("fahrenheit:",(c*9/5)+32)
f=float(input("enter fahrenheit:"))
print("celsius:" ,(f-32)*5/9)
print("successfully converted temperature into celsius and fahrenheit")
f=float(input("enter fahrenheit:"))
print("celsius:" ,(f-32)*5/9)
print("successfully converted temperature into celsius and fahrenheit")
f=float(input("enter fahrenheit:"))
print("enter valid one")
Output:

enter temperature in celcius: 34


fahrenheit: 93.2
enter fahrenheit:40
celsius: 4.444444444444445
successfully converted temperature into celsius and fahrenheit
enter fahrenheit:32
celsius: 0.0
successfully converted temperature into celsius and fahrenheit
enter fahrenheit:45
enter valid one
GONE MADHUSRI
CSD-A
25255A6704

3. Write a Python program that prints prime numbers less than 20(using
for-else).

Program:

a=int(input("enter your number: "))

print("print numbers between 1 and" ,a, "are:")

for i in range(2,a+1):

for j in range(2,i):

if i % j==0:

break

else:

print(i)

Output:

Enter your number20


Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19
GONE MADHUSRI
CSD-A
25255A6704

4. Write a Python program to construct the following pattern, using a


nested for loop.

1
22
333
4444
55555
4444
333
22
1
Program:
a=5

for i in range(1,a+1):

for j in range(i):

print(i,end=" ")

print( )

for i in range(a-1,0,-1):

for j in range(i):

print(i,end=" ")

print( )

Output:
1
22
333
4444
55555
4444
333
1 2
1
GONE MADHUSRI
CSD-A
25255A6704

5. Write a program to get the binary form of a given number.

Program:

def to_binary(a):

binary=""

while a>0:

rem=a%2

binary=str(rem) + binary

a//=2

return binary

print(to_binary(8))

print(to_binary(18))

print(to_binary(7))

Output:

1000

10010

111

You might also like