0% found this document useful (0 votes)
5 views6 pages

Rainy

hello world

Uploaded by

rainy.budhathoki
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)
5 views6 pages

Rainy

hello world

Uploaded by

rainy.budhathoki
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
You are on page 1/ 6

11/3/24, 4:04 PM Rainy

Rainy Budhathoki

Presidential Graduate School, Kathmandu

PRG 330: Python Programming with Data

Prof. Tek Raj Pant

Nov 3, 2024

For Loop
Iterate over list, range (enumarator)

In [ ]: numbers = [1,1,2,3,4,5,3,11,100]

even_list = []
# iterate over list
for num in numbers:
print (num)

1
1
2
3
4
5
3
11
100

In [ ]: # To find the even and odd numbers from the given numbers

numbers=[1,1,2,3,4,5,3,1,100] #list of int


even_list=[]
odd_list=[]

#iterate over list for num in numbers:


for num in numbers:
if num % 2==0:
print (num)
even_list.append(num)
else:
odd_list.append(num)
print(even_list)
print(odd_list)

if 2 in numbers: #check item exists in list


print('2 exists in numbers')

file:///C:/Users/Administrator/Downloads/Rainy.html 1/6
11/3/24, 4:04 PM Rainy

2
4
100
[2, 4, 100]
[1, 1, 3, 5, 3, 1]
2 exists in numbers

In [ ]: # To create a list of unique numbers from the given list

numbers=[1,1,2,3,4,5,3,1,100,2,4,7,9,100]
unique_num=[]

for num in numbers:


if num not in unique_num:
unique_num.append(num)
print(unique_num)

[1, 2, 3, 4, 5, 100, 7, 9]

In [ ]: # For loop in strings

sentence = "Hello World"


for char in sentence:
print (char)

H
e
l
l
o

W
o
r
l
d

In [ ]: # To count the total number of vowels in given string

sentence = "baidehi basnet"


vowels = "a,e,i,o,u"
total_vowels = 0
for char in sentence:
if char in vowels:
total_vowels +=1
print (total_vowels)

In [ ]: # To print the total nummber of consonents

sentence = "baidehi basnet"


vowels = "a,e,i,o,u"
total_vowels = 0
for char in sentence:
if char not in vowels:

file:///C:/Users/Administrator/Downloads/Rainy.html 2/6
11/3/24, 4:04 PM Rainy

total_vowels +=1
print (total_vowels)

In [ ]: # Find sum of all items in given list

numbers = [100,1,-10,5]
sum=0
for num in numbers:
sum += num
print(sum)

96

Range
Range(start,stop,step)

In [ ]: range_of_100 = range(1,10)
sum=0
for i in range(1,11): #for i=0; i<10, i++
print(i)
sum+=i

1
2
3
4
5
6
7
8
9
10

Inline for Loop


In [ ]: numbers = [1,2,3,5,6,88,19]
transformed_list = [(num *100 if num%2==0 else num) for num in numbers ]
print(transformed_list)

[1, 200, 3, 5, 600, 8800, 19]

Def function_name (parameters)

block of code

file:///C:/Users/Administrator/Downloads/Rainy.html 3/6
11/3/24, 4:04 PM Rainy

return expression
In [ ]: #finding the sum of two numbers

def add(x,y):
sum = x+y
return sum
sum = add(x=5, y=10)
print(sum)

15

In [ ]: #finding the product of two numbers

def product(x,y):
product = x*y
return product
prod = product(5,10)
print(prod)

50

In [ ]: # Write a function # Write a function that accepts three parameters (x, y, operatio
# based on the operation (+, -, *, /)
# Perform addition, subtraction, multiplication, and division
# and then return the value of x operation y

def calculate(x, y, op):


if op == '+':
return x + y
elif op == '-':
return x - y
elif op == '*':
return x * y
elif op == '/':
if y != 0:
return x / y
else:
return "Invalid operation"

sum_result = calculate(1, 2, '+')


print(sum_result)
division = calculate(1, 1, '/')
print(division)

3
1.0

In [ ]: # Establishing IP connection

def connect(ip="127.0.0.1"):
print(f"Hello connecting to {ip}")
connect()

Hello connecting to 127.0.0.1

file:///C:/Users/Administrator/Downloads/Rainy.html 4/6
11/3/24, 4:04 PM Rainy

In [ ]: # Establishing IP connection by changing it

def connect(ip="12.12.3.4"):
print(f"Hello connecting to {ip}")
connect("12.12.3.4")

Hello connecting to 12.12.3.4

In [ ]: def connect(ip="12.12.3.4"):
print(f"Hello connecting to {ip}")

connect("12.12.3.4")

def sum (x=0,y=0):


return x+y

add=sum(1)
print(add)

Hello connecting to 12.12.3.4


1

Return Statement:
functions can return values after using this statement
if no return statement is used, the function returns None by default

In [ ]: def is_even_number (number):


if number % 2 == 0:
return "number is even" #after no return code works
return "number is odd"

Lambda Function
Lambda function (anonymous function) are small, single-expression functions defined
by lambda keyboard
They are often used for short, simple operations

In [ ]: square = lambda x: x**2


print (square(4))

Extra Questions
In [ ]: # Write a function to swap two numbers without using a temporary variable

def swap_numbers(a, b):


a = a + b # Swap values
b = a - b

file:///C:/Users/Administrator/Downloads/Rainy.html 5/6
11/3/24, 4:04 PM Rainy

a = a - b
print(a, b)
return a, b

a, b = swap_numbers(5, 10)
print(a, b)

10 5
10 5

In [ ]: # Write a function to count the number of vowels in a given string

def count_vowels(sentence):
total_vowels = 0
vowels = "aeiouAEIOU"
for char in sentence:
if char in vowels:
total_vowels += 1
return total_vowels

vowel_count = count_vowels("Hello World")


print(vowel_count)

In [ ]: # Write a function to check if a number is prime or not

def prime(number):
is_prime = True
for i in range(2, int(number / 2) + 1):
print(i) # Numbers less than or equal to 1 are not prime
if number % i == 0: # Check up to the square root of the number
is_prime = False
break # If condition matches, then loop breaks (break = earl
return is_prime
print(prime(16))

2
False

file:///C:/Users/Administrator/Downloads/Rainy.html 6/6

You might also like