Class 7 - LOOPS & FUNCTIONS IN PYTHON

Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Class 7 - LOOPS & FUNCTIONS IN PYTHON Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

LOOPS & FUNCTIONS IN PYTHON

Notes from the AI Basic Course (Class 7) by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

When you start learning it time takes to absorb because learning is a continous process.

Learning is your own Responsibility.

Don't be Neutral in Life. Stand with the Truth.

What is Loop ?

A loop is a programming concept that allows for the repetition of a set of instructions multiple times.

# print the Hello World using the print statement

print(" Hello World")

print(" Hello World")

print(" Hello World")

print(" Hello World")

print(" Hello World")

print(" Hello World")

print(" Hello World")

print(" Hello World")

print(" Hello World")

print(" Hello World")

# print the string specific number of times

for i in range(10):

print("Hello World")

print("Hello Pakistan")

# Print each fruit in a fruit list:

fruits = ["apple", "banana", "cherry"]

for item in fruits:

print(item)

# printing the List

print(fruits)

# Print each value in a my_list, list:

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

for number in my_list: #<--- x is a variable, who's scope is limited to the for loop. x can be named anything you'd like

print(number)

Looping Through a String:

Even strings are iterable objects, they contain a sequence of characters:

Loop through the letters in the word "banana":

for chars in "banana":

print(chars)

The range() function:

for i in range(8):

print(i)

# Printing Numbers

for num in range(1, 6):

print(num)

# Using the For loop to print the Items of list and their index

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

for i in range(len(my_list)):

print(i, my_list[i])

# Getting the Length of List using the len() function

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

len(my_list)

Looping through a dictionary:

# printing the values from the Dictionary

my_dict = { "brand": "Ford", "model": "Mustang", "year": 1964, "year": 2020}

for value in my_dict.values():

print(value)

# printing the keys from the dictionary

my_dict = { "brand": "Ford", "model": "Mustang", "year": 1964, "year": 2020}

for key in my_dict.keys():

print(key)

# Finding Even Numbers

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for num in numbers:

if num % 2 == 0:

print("number is even.", num)

Python While Loops:

# Print i as long as i is less than 60:

i = 1

while i <= 60:

print(i)

i = i + 1

# while loop

count = 0

while (count < 3):

print("Print hello", count)

count = count + 1

# User Input Validation

password = ""

while password != "secret":

password = input("Enter the password: ")

if password == "secret":

print("Access granted!")

else:

print("Access denied!")

# Summing Numbers

total = 0

num = 1

while num <= 10:

total = total + num

num = num + 1

print("The sum is:", total)

Python Functions:

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

Creating a Function:

#syntax

def function_name():

def my_function():

print("Hello from a function")

Calling a Function:

def my_function():

print("Hello from a function")

my_function()

Arguments:

Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:

# Function with Parameters

def add_numbers(a, b):

sum = a + b

return sum

# Calling the function

result = add_numbers(5, 3)

print("The sum is:", result)

Parameters or Arguments?

The terms parameter and argument can be used for the same thing: information that are passed into a function.

def my_function(food):

for x in food:

print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)

Return Values:

def my_function(x):

return 5 * x

print(my_function(3))

print(my_function(5))

print(my_function(9))

# Function with Default Parameter

def greet_person(name="World"):

print("Hello,", name)

# Calling the function without an argument

greet_person()

# Calling the function with an argument

greet_person("Alice")

# python program to find Even and Odd Numbers

def check_even_odd(num):

if num % 2 == 0:

print(num, "is even")

else:

print(num, "is odd")

# Call the function

check_even_odd(7)

# Python Program to Count the Number of Words in a String:

def count_words(string):

words = string.split()

return len(words)

# Call the function

sentence = "Python is a popular programming language"

word_count = count_words(sentence)

print("Number of words:", word_count)

Loops:

For & While

FOR is used for repition of tasks.

range is built-in function.

in is allign with for loop

If you want to grow in Life, Self Learning is the Key.

len() is length function for collection

Functions are very important in Python. Only runs, when it is called.

#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #hamzanadeem

要查看或添加评论,请登录

Hamza Nadeem的更多文章

社区洞察

其他会员也浏览了