0% found this document useful (0 votes)
25 views38 pages

05-List and Loop-1

Here are the key steps to solve this problem using a while loop: 1. Initialize variables to track username and password 2. Use a while loop to continuously check if username and password match 3. Inside the loop, prompt for username and password and check for match 4. If no match, print "invalid" 5. If match, break out of the loop 6. Outside the loop, print "Valid" This allows repeating the username and password prompts until a valid match is entered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views38 pages

05-List and Loop-1

Here are the key steps to solve this problem using a while loop: 1. Initialize variables to track username and password 2. Use a while loop to continuously check if username and password match 3. Inside the loop, prompt for username and password and check for match 4. If no match, print "invalid" 5. If match, break out of the loop 6. Outside the loop, print "Valid" This allows repeating the username and password prompts until a valid match is entered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Python List Data Type

• List is an ordered collection of similar or different types of items


separated by commas and enclosed within brackets [ ]. For
example,

• languages = [“C", "Java", "Python"]

• Here, we have created a list named languages with 3 string values


inside it.
Access List Items

• To access items from a list, we use the index number (0, 1, 2 ...). For
example,

languages = [“C", "Java", "Python"]


print(languages[0])
print(languages[2])

• In the above example, we have used the index values to access


items from the languages list.

• languages[0] - access first item from languages i.e. C


• languages[2] - access third item from languages i.e. Python
Python List Data Type

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


print(thislist)

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


print(len(thislist))
3
Range()

range(5)
Out[1]: range(0, 5)

list(range(5))
Out[8]: [0, 1, 2, 3, 4]
Loop

• In computer programming, loops are used to repeat a block of


code.

• For example, if we want to show a message 100 times, then we


can use a loop. It's just a simple example; you can achieve much
more with loops.

• There are 2 types of loops in Python:

• for loop

• while loop
Flowchart of Python for Loop
Python for Loop

• A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).

• With the for loop we can execute a set of statements, once for each item
in a list, tuple, set etc.

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


for x in fruits:
print(x)

apple
banana
cherry
Python for Loop

for x in “Apple":
print(x)

A
p
p
l
e
The range() Function

• To loop through a set of code a specified number of times, we can use


the range() function,

• The range() function returns a sequence of numbers, starting from 0 by default,


and increments by 1 (by default), and ends at a specified number.

for x in range(6):
print(x)

0
1
2
3
4
5

Note that range(6) is not the values of 0 to 6, but the values 0 to 5.


Python for Loop

x = int(input("Enter range : "))

for i in range(x):
print(i)

Enter range : 5
0
1
2
3
4
Python for Loop

• The range() function defaults to 0 as a starting value, however it is


possible to specify the starting value by adding a parameter: range(2, 6),
which means values from 2 to 6 (but not including 6):

for x in range(2, 6):


print(x)

2
3
4
5
Python for Loop

for x in range(10,0,-1):
print(x)

10
9
8
7
6
5
4
3
2
1
What does end =‘ ’ do in Python?

• The end parameter in the print function is used to add any string. At the end of the output of the print
statement in python.
• By default, the print function ends with a newline.
• Passing the whitespace to the end parameter (end=‘ ‘) indicates that the end character has to be
identified by whitespace and not a newline.

print("Comsats", end=' ' )


print("University")
Comsats University

print("Comsats")
print("University")
Comsats
University

• (Ctrl+Enter)
Python for Loop

for x in range(10,0,-1):
print(x, end=' ')

10 9 8 7 6 5 4 3 2 1
Python Nested Loops

• Loops Inside Loops


• A nested loop is a loop inside a loop.
• The "inner loop" will be executed one time for
each iteration of the "outer loop":

Outer_loop Expression:
Inner_loop Expression:
Statement inside inner_loop
Python Nested Loops

https://www.geeksforgeeks.org/python-nested-loops/
Python Nested Loops

x = [1, 2]
y = [4, 5]

for i in x:
for j in y:
print(i, j)

1 4
1 5
2 4
2 5
Python Nested Loops

for x in range(5):
print("Value of x:",x)
for y in range(1,5):
print("Value of y:",y,end=", ")
print("\n")
Value of x: 0
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,

Value of x: 1
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,

Value of x: 2
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,

Value of x: 3
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,

Value of x: 4
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,
Python Nested Loops
Value of x: 0
Value of y: 1
Value of y: 2
Value of y: 3
Value of y: 4

for x in range(5): Value of x: 1


Value of y: 1
Value of y: 2
print("Value of x:",x) Value of y: 3
Value of y: 4
for y in range(1,5):
print("Value of y:",y) Value of x: 2
Value of y: 1
Value of y: 2
print("\n") Value of y: 3
Value of y: 4

Value of x: 3
Value of y: 1
Value of y: 2
Value of y: 3
Value of y: 4

Value of x: 4
Value of y: 1
Value of y: 2
Value of y: 3
Value of y: 4
Python Nested Loops

for i in range(10):
for j in range(i):
print("*",end=' ')
print(" ")

*
**
***
****
*****
******
*******
********
*********
Python Nested Loops

for i in range(10):
for j in range(i):
print(j ,end=' ')
print(" ")

0
01
012
0123
01234
012345
0123456
01234567
012345678
Python Nested Loops

for i in range(10,0,-1 ):
for j in range(i):
print(j ,end=' ')
print(" ")

0123456789
012345678
01234567
0123456
012345
01234
0123
012
01
0
Python Nested Loops

x = [1, 2]
y = [4, 5]

for i in x:
for j in y:
print(i, j)

1 4
1 5
2 4
2 5
lst = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
var = int(input())
lst.append(var)
print(lst)
Enter number of elements : 2

Enter Your Name :a

Enter Your Age :20

Enter Your Name :b

Enter Your Age :30


['a', 'b']
[20, 30]
Age = []
Name= []
n = int(input("Enter number of elements : "))
for i in range(0, n):
nameVar = input("Enter Your Name :")
ageVar = int(input("Enter Your Age :"))
Name.append(nameVar)
Age.append(ageVar)
print(Name)
print(Age)
The while Loop

• With the while loop we can execute a set of statements as long as a


condition is true.

Python while loop is used to run a block code until a certain


condition is met.
The syntax of while loop is:
while condition: # body of while loop
While Loop

• A while loop evaluates the condition


• If the condition evaluates to True, the code inside the while loop is
executed.
• condition is evaluated again.
• This process continues until the condition is False.
• When condition evaluates to False, the loop stops.
While Loop
While Loop

name = input("Enter your name :")


if name=="":
print("You have not enter the name")
else:
print("Hello", name)
While Loop

name = input("Enter your name :")


while name=="":
print("You have not enter the name")
name = input("Enter your name :")
print("Hello", name)
• Enter your name :
• You have not enter the name

• Enter your name :


• You have not enter the name

• Enter your name :aaa


• Hello aaa
While Loop

age = int(input("Enter your Age :"))


while age < 0:
print("Age cannot be in negative")
age = int(input("Enter your name :"))
print("Hello : you are ", age, "years old")

Enter your Age :-12


Age cannot be in negative

Enter your name :25


Hello : you are 25 years old
While Loop

orderList=[]
order = input("place your order :")
while not order == "q":
orderList.append(order)
order = input("place your order or press Q for quit :")
print("Hello : you order ", orderList)
While Loop

i=1
while i < 6:
print(i)
i = i+ 1

1
2
3
4
5
i=1
while i in range(0,10):
print("Hello world", i)
i=i+1
• Hello world 1
• Hello world 2
• Hello world 3
• Hello world 4
• Hello world 5
• Hello world 6
• Hello world 7
• Hello world 8
• Hello world 9
While Loop

username = input("Enter your Username :")


password = input("Enter your password :")

while not username=="admin" or not password=="admin":


print("invalid ")
username = input("Enter your Username :")
password = input("Enter your password :")

print("Valid")
While Loop

username = input("Enter your Username :")


password = input("Enter your password :")

while username!="admin" or password!="admin":


print("invalid ")
username = input("Enter your Username :")
password = input("Enter your password :")

print("Valid")
Sal = []
Tax= []
n = int(input("Enter number of elements : "))
for i in range(0, n):
salVar = int(input("Enter Your Sal :"))
Sal.append(salVar)
taxVar=salVar*.25
Tax.append(taxVar)

print(Sal)
print(Tax)

print(list(zip(Sal,Tax)))

You might also like