0% found this document useful (0 votes)
22 views

Python c4 - c7

Uploaded by

piyasad78
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)
22 views

Python c4 - c7

Uploaded by

piyasad78
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/ 15

Python Nested Loop

''' Print No 0 to 10'''

for i in range(0, 10+1):


print(i,end=" ")

''' Print No 10 to 1'''

for i in range(10, 0, -1):


print(i,end=" ")

''' Print even no only 1-10'''

for i in range(0, 10+1 , +2):


print(i,end=" ")

''' Print Odd no only m-n'''

m= int(input("Enter start: "))


n= int(input("Enter end: "))

for i in range(m,n+1):
if(i%2 != 0):
print(i,end=" ")

'''Prime No'''
n=int(input("Enter no: "))
c=0
for i in range(2,n):
if(n%i==0):
print("Not Prime No ");
break;
else:
print("Prime No")

'''
*
**
***
****
*****
'''

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


for i in range(1,n+1):
for j in range(1,i+1):
print('*',end=' ')
print()

'''
*****
****
***
**
*

54321
4321
321
21
1
'''

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


for i in range(n,1-1 ,-1):
for j in range(i,1-1 ,-1):
print(j,end=' ')
print()

'''
*****
****
***
**
*

54321
5432
543
54
5
'''
n= int(input("Enter range: "))
for i in range(0, n+1, +1):
for k in range(1, i+1, +1):
print(" ",end="")

for j in range(n, i ,-1):


print(j,end="")

print()
'''
*
**
***
'''
n= int(input("Enter range: "))
for i in range (0, n+1, +1):
for k in range(n, i-1, -1):
print(" ",end="")
for j in range(0, i+1, +1):
print("* ",end="")
print()

"""
12345
2
3
4
5
"""
n= int(input("Enter range: "))
for i in range(1, n+1, +1):
if i==1:
for j in range(1, n+1, +1):
print(j,end=" ")
print()
continue
print(i)
"""

*
**
***
**
*
"""

n= int(input("Enter Number: "))


for i in range(1, n+1, +1):
for k in range(n, i, -1):
print(" ", end="")
for j in range(1, i+1, +1):
print("*",end=" ")
print()

for i in range(1, n+1, +1):


for k in range(1, i+1 , +1):
print(" ", end="")
for j in range(n-1, i-1, -1):
print("*",end=" ")

print()
*
*
*
*
*

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


for i in range(0, n+1, +1):
for k in range(1, i+1, +1):
print(" ",end="")
for j in range(0, 1, +1):
print("*",end="")
print()

"""
*
***
******
**********
***************

"""

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


for i in range (1,n+1, +1):
for k in range(1,i+1, +1):
for j in range(1, k+1, +1):
print("j",end=" ")
print()

""" *****
*****
*****
*****
***** """

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


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

______
| |
| |
| |
_______
n= int(input("Enter range: "))
for i in range(n):
for j in range(n):
if (i==0 or j==0 or i==n-1 or j==n-1):
print("*",end="")
else:
print(" ",end="")
print()

""" 1
01
101
0101
10101
"""
n= int(input("Enter range: "))
for i in range(1,n+1):
for j in range(1,i+1):
sum= i+j
if(sum%2 == 0):
print("1",end="")
else:
print("0",end="")
print()

"""
* Pattern 1
**
***
****
* Pattern 2
**
***
****
*****
"""

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


for i in range(1,n+1):
for k in range(n, i, -1):
print(" ", end= "")
for j in range(1,i+1):
print("*",end="") # (for pattern 2)----> print("* ",end="")
print()

#ASCII

n='A'
m='Z'
print(ord(n)+1)
print(ord('Z'))

for i in range(ord(n),ord(m)+1):
print(chr(i)," ------> ", i)

#Type Casting
n= '12'
n= int(n)
print(type(n))

Python Collection

Collection of Python have 5 main collections, List Tuple Set &


Dictionaries:

- LIST-
- Ordered Collection of Item
- Mutable(Can be Modified)
- Created using Square Brackets [] or list() function
- Element can be accessede by index
- It supports Slicing
- It Supports iteration
- It is heterogeneous( different types of data )

my_list= [1,2,3,4,5]
print(my_list)

- TUPLE-
- Ordered Collection of items.
- Imutable(Cannot be Modified)
- Created using Parenthesis ()
- Element can be accessed by Index
- It Supports Slicing
- It is heterogeneous( different types of data )
- It is memory efficient becouse it consume less memory comapare to
list

my_tuple = (1,2,3,4,5)
value= my_tuple[0]
print(value)

- SET-
- Unorderd collection of items
- All the items must be unique & distinct
- Mutable
- Created using Curley Braces {} or set()
- Does not support indexing or slicing.

my_set = {1,2,3,4,5}
my_set2= set([2,3,1,4,5]) #Converting List----> Set
print(my_set == my_set2)

- DICTIONARY-
- Collection of Key-value-pairs
- Mutable
- Created using Curley Braces {} or dict() function
- Element can be accessed only using Keys
- Keys in a dictionary must be unique, but, values can be duplicated
- Supports iteration over Keys, Valyes or Items.

d1 = {'name': 'Alice', 'age': '30', 'city': 'New York'}


print(d1['name'])
d1['age']= 31

You might also like