0% found this document useful (0 votes)
2 views2 pages

Python Functions and Loops Examples

The document contains several Python code snippets demonstrating basic programming concepts. It includes a program to calculate the sum of numbers from 1 to 100 using a while loop, functions to find the maximum of two numbers, check if a number is even, count vowels in a string, and count uppercase letters in a string. Each code snippet is accompanied by example outputs.

Uploaded by

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

Python Functions and Loops Examples

The document contains several Python code snippets demonstrating basic programming concepts. It includes a program to calculate the sum of numbers from 1 to 100 using a while loop, functions to find the maximum of two numbers, check if a number is even, count vowels in a string, and count uppercase letters in a string. Each code snippet is accompanied by example outputs.

Uploaded by

Taveed Ghazarian
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#Write a program to calculate the sum of all the numbers from 1 to 100

#using a while loop.

i=1
sum=0
while i<=100:
sum+=i
i+=1
print(sum)

_________________________________________________________________________

#Write a function that takes two numbers as arguments and returns the
largest of them.
def max(x=1 , y=4):
if x>y:
return x
else:
return y
print(max(),"is max number")# 4 max is max number
#or
print(max(7,6)," is max number")# 7 max is max number

#Write a function that takes a number as an argument and returns True if


the number is even, and False if it is odd.
def check(x=4):
if x%2==0:
return True
else:
return False
print(check())#True
#or
print(check(7))#False

B-M-T-H
#Write a function that takes a string as an argument and returns the
number of vowels in the string.
def vowels(x='hello'):
vowels = "aeiouAEIOU"
count = 0
for i in x:
if i in vowels:
count += 1
return count

print('the number of vowel are',vowels())#2


#or
print('the number of vowel are',vowels('aeiou'))#5

_________________________________________________________________________________

#Write a function that takes a string as an argument


#and returns the number of uppercase letters in the string.
def uper(x='Hello World'):
count=0
for i in x:
if i>='A' and i<='Z':
count+=1
return count
print(uper())#2
#or
print(uper('ABCd'))#3

#Write a program to calculate the sum of all the numbers from 1 to 100
#using a while loop.

i=1
sum=0
while i<=100:
sum+=i
i+=1
print(sum)

B-M-T-H

You might also like