0% found this document useful (0 votes)
13 views4 pages

Lec 3

The document contains Python code demonstrating basic programming concepts such as variable types, control structures (if-else statements, loops), functions, and file handling. It includes examples of arithmetic operations, string manipulations, and the Fibonacci sequence. Additionally, it showcases how to write to and read from a file.

Uploaded by

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

Lec 3

The document contains Python code demonstrating basic programming concepts such as variable types, control structures (if-else statements, loops), functions, and file handling. It includes examples of arithmetic operations, string manipulations, and the Fibonacci sequence. Additionally, it showcases how to write to and read from a file.

Uploaded by

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

print("Hello")

a = 10 #int
b = 5.5 #Float
c = "UMT" #string

print(a, type(a))
print(b, type(b))
print(c,type(c))
width = 10
height = 5
print(width*height)

size = width * height


print(size)

Hello
10 <class 'int'>
5.5 <class 'float'>
UMT <class 'str'>
50
50
Hello
10 <class 'int'>
5.5 <class 'float'>
UMT <class 'str'>
50
50

v1 = 20
v2 = 20
if v1<v2:
print("TRUE")
elif v1>v2:
print("Condition 2")
else:
print("False")

False

#Loops

for i in range(1,10):
print(i)
print("LOOP")

#a = 10; n = 1
a,n = 10,1
while n<=a:
print("n = ", n)
n += 1;

words = ['A', 'Bb', 'Ccccc', 'Dddd', 'Eee', 'Ffff', 'Gg']


for i in words:
print(i, type(i), len(i))

1
LOOP
2
LOOP
3
LOOP
4
LOOP
5
LOOP
6
LOOP
7
LOOP
8
LOOP
9
LOOP
n = 1
n = 2
n = 3
n = 4
n = 5
n = 6
n = 7
n = 8
n = 9
n = 10
A <class 'str'> 1
Bb <class 'str'> 2
Ccccc <class 'str'> 5
Dddd <class 'str'> 4
Eee <class 'str'> 3
Ffff <class 'str'> 4
Gg <class 'str'> 2

def statement ():


print("HEELO WORLD")
print("PYTHON")
print("AI")
statement()

def addition(a,b):
c = a + b
print("Sum = ", c)

v1 = 15; v2 = 23
addition(10,20)
addition (v1,v2)

def subtraction(a,b):
c = a - b
return c

print("Sub = ",subtraction(v1,v2))
sub = subtraction(120,200)
print("sub = ", sub)

HEELO WORLD
PYTHON
AI
Sum = 30
Sum = 38
Sub = -8
sub = -80

def fib(n):
a,b=0,1
i=1
while i<=n:
print(a, end=" ")
a,b=b,a+b
i+=1

fib(70)

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765


10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169
63245986 102334155 165580141 267914296 433494437 701408733 1134903170
1836311903 2971215073 4807526976 7778742049 12586269025 20365011074
32951280099 53316291173 86267571272 139583862445 225851433717
365435296162 591286729879 956722026041 1548008755920 2504730781961
4052739537881 6557470319842 10610209857723 17167680177565
27777890035288 44945570212853 72723460248141 117669030460994

def Greetings(fname, lname, formal = True):


if formal:
return "Mr. %s %s" %(fname, lname)
else:
return "Hello %s %s" % (fname,lname)

print(Greetings("ABC", "DEF"))
print(Greetings("XYZ", "123", False))
Mr. ABC DEF
Hello XYZ 123

with open('myfile.txt', 'w') as myfile:


print("Hello", file = myfile)
print("World", file = myfile)
print("UMT AI Course", file = myfile)

with open('myfile.txt', 'a') as myfile:


print("123", file = myfile)
myfile.write("AI LAB")
print(" ")

with open('myfile.txt', 'r') as myfile:


data = myfile.read()
print(data)

Hello
World
UMT AI Course
123
AI LAB123
AI LAB123
AI LAB123
AI LAB123
AI LAB

You might also like