Lecture 7 - Loop & Functions in Python

 

for i in range(8):

print(i)

 

0

1

2

3

4

5

6

7

for num in range(1,7):

 

print(num)

 

1

2

3

4

5

6

my_list =[10,20,30,40,50,60]

for i in range(len(my_list)):

 

print(i,my_list[i])

 

0 10

1 20

2 30

3 40

4 50

5 60

 

 

Return Funtion

def my_function(x):

   return 3*x

print(my_function(3))

15

Def my_greetings(name = “World”):

   Print(“Hello”, name)

my_greetings()

Hello World

def greetings(name = "World"):

  print("Hello" , name)

 

greetings("Tahir")

 

Hello Tahir

 

 

def check_even_odd(num):

  if num % 2==0:

    print(num ,"is even ")

  else:

    print(num, "is odd")

 

check_even_odd(7)

 

7 is odd

 

 

check_even_odd(5000)

 

5000 is even

 

 

def count_words(string):

  words = string.split()

  return len(words)

 

sentence="Pakistan is a beautiful country"

word_count = count_words(sentence)

print("The Number of Words are" , word_count)

 

The Number of Words are 5

 

 

 

Comments

Popular posts from this blog

Topic-18 | Evaluation Metrics for different model

Topic 22 | Neural Networks |

Topic 17 | Linear regression using Sklearn