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

21 Ec 034

The document contains several code snippets demonstrating basic programming concepts in Python. It includes examples of a phonebook search, tuple concatenation, file copying, a rectangle class for area and perimeter calculations, vowel counting in a sentence, list indexing, Fibonacci sequence generation, and regex pattern matching. Each code snippet is followed by its expected output.

Uploaded by

harshithgowdah88
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 views4 pages

21 Ec 034

The document contains several code snippets demonstrating basic programming concepts in Python. It includes examples of a phonebook search, tuple concatenation, file copying, a rectangle class for area and perimeter calculations, vowel counting in a sentence, list indexing, Fibonacci sequence generation, and regex pattern matching. Each code snippet is followed by its expected output.

Uploaded by

harshithgowdah88
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/ 4

1]

phonebook = {

"Raj": "8747010488",

"Alice": "8088098227",

"Bob": "7349323784"

key = input("Enter a name to search for: ")

if key in phonebook:

print(f"The phone number for {key} is {phonebook[key]}")

else:

print("Key not found")

output:

Enter a name to search for: Alice

The phone number for Alice is 8088098227

2]

tuple1=['harshi'];

tuple2=['gowda'];

tuple3=tuple1+tuple2;

print(tuple3);

output:

['harshi', 'gowda']

4]

def copy_file(source,destination):

with open(source,'r') as src:

with open(destination,'w') as dest:

dest.write(src.read())

sfile='source.txt'

dfile='destination.txt'
copy_file(sfile,dfile)

5]

class Rectangle():

def __init__(self,l,w):

self.length=l

self.width=w

def rectangle_area(self):

return self.length*self.width

def rectangle_peri(self):

return 2*(self.length+self.width)

newRectangle=Rectangle(12,20)

print('area',newRectangle.rectangle_area())

print('perimeter',newRectangle.rectangle_peri())

output:

area 240

perimeter 64

6]

sentence = "Hello, World! How are you"

def count_vowels(sentence):

vowels = "aeiou"

count = 0

for char in sentence:

if char.lower() in vowels:

count += 1

return count

print(count_vowels(sentence))

output:8

7] num=[10,20,30,40]

print(num.index(40))
output:3

8]

nterms = int(input("How many terms? "))

n1, n2 = 0, 1

count = 0

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < nterms:

print(n1)

nth = n1 + n2

n1 = n2

n2 = nth

count += 1

output:

How many terms? 8

Fibonacci sequence:

13
9] import re

pattern = r'^[A-Z][a-z]+$'

strings = ["Hello", "Apple", "Zebra", "hello", "HELLO", "HelloWorld"]

for s in strings:

if re.match(pattern, s):

print(f"Matching: {s}")

else:

print(f"Not matching: {s}")

output:

Matching: Hello

Matching: Apple

Matching: Zebra

Not matching: hello

Not matching: HELLO

Not matching: HelloWorld

You might also like