Please Subscribe My YouTube Channel

Thursday, March 16, 2023

Python Program : List Method

Python Program : List Method

#list is a collection of diffrent types of data.
#enclosed with square brackets
#list elements seperated by comma
#It may be empty
#for creating a list we use list construtor or method
#list is mutable or changable (we can change / add/ remove / access list items)


# create an empty list
list1= []
print(list1)

#list with mixed data types
#with int string and float, tuple, list and set value.      

list2 = [1, "Hello", 3.4, (1,2,4), [1,3,4], {1,2,3}] 
print(list2)

#creating list with list constructor
tl= list(("apple", "banana", "cat"))
print(tl) #['apple', 'banana', 'cat']

#Acess list items
plang = ["Python", "Swift", "C++","Kotlin","C","C#"]

# access item at index 0
print(plang[0])# result:- Python

# access item at index 2
print(plang[2])   # C++
# access item at index 5
print(plang[5])   # C#

# access item at negative index -1
print(plang[-1])   # C#
# access item at negative index -2
print(plang[-2])   # C#

#access nested list element
list1=[5,3.2,'ram',6,8,9,[8,5,6]]

print(list1[6][1]) #5

#List slicing in Python

my_list = ['I','T','C','I',' ','H','A','R','D','O','I']

#items from index 2 to index 4
print(my_list[0:4]) #result- ['I','T','C','I']

# items from index 5 to end
print(my_list[5:]) #result:- ['H','A','R','D','O','I']
# result items beginning to end
print(my_list)
print(my_list[:])
print(my_list[:11])
print(my_list[-11:])


# Add element using append method in list
numbers= [21, 34, 54, 12]
print("Before Append:", numbers)
# after using append method
numbers.append(32) #it takes exactly one argument in one time
print("After Append:", numbers)

#with extend method you can add a list element in another list (join two lists)
#using extend method here you can also add any iterable object (tuples, sets, dictionaries etc.).
num1 = [2, 3, 5]
print("List1:", num1)
num2 = [4, 6, 8]
print("List2:", num2)
num2.extend(num1)
print("List after extend:", num2)

#or
fr_list = ["apple", "banana", "cherry"]
other_list = list(fr_list)
print(other_list)

#or using + concatenate operator
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

#Creating nested list
list1 = [1, 2, 3, 4, 5]
print(list1)
list2 = [12, 13, 23]
print(list2)
list3 = [10, 20, 30]
print(list3)
NestedList = [list1, list2, list3, list4, list5]
print("The list of lists or Nested list is:")
print(NestedList)

#copy list items in another empty list
g= ["apple", "baby", "ambuj"]
a = g.copy()
print(a)

#or you can copy
g= ["anand", "vartika", "chaya"]
a = list(g)
print(a)



#insert an item in list without removing existing element
thislist = ["apple", "banana", "cow"]
thislist.insert(2, "water") 
print(thislist) # here result will be ["apple", "banana", "water","cow"]

# changing list item:(it's mutable so we can change list items.
color= ['pink', 'red', 'gray']

# changing the third item to 'C'
color[2] = 'green'

print(color)  # ['pink', 'red', 'green']

# changing the list items acc to range(change range)
th = ["apple", "blue", "cool", "orange", "kiwi", "mango"]
print(th) #["apple", "blue", "cool", "orange", "kiwi", "mango"]
th[1:3] = ["black", "bag"]
print(th) #["apple", "black", "bag", "orange", "kiwi", "mango"]

#pop method for using last element from list or remove item acor to index value
t = ["apple", "banana", "cherry"]
t.pop()
print(t) #["apple", "banana"]

t1=["ajay", "vijay", "chetan"]
t1.pop(2)
print(t1)

#remove method for remove any specific element
prog = ['Python', 'Swift', 'C++', 'C', 'Java', 'Ruby', 'php']
prog.remove('Java')
print(prog)

#remove list items using del keyword
prog = ['Python', 'Swift', 'C++', 'C', 'Java', 'Ruby', 'php']

# deleting the second item
del prog[1]
print(prog) # ['Python', 'C++', 'C', 'Java', 'Ruby', 'php']

# deleting the last item
del prog[-1]
print(prog) # ['Python', 'C++', 'C', 'Java', 'Ruby']

# delete first two items
del prog[0 : 2]  # ['C', 'Java', 'Ruby']
print(prog)


# del all list in one time with del and clear
prog = ['Python', 'Swift', 'C++', 'C', 'Java', 'Ruby', 'php']
del prog

prog1= ['Python', 'Swift', 'C++', 'C']
prog1.clear()
print(prog1) #return empty list

#creating a list with for loop
for i in range(6):
         print(i)
#print all items from list by using loop method
z=['a','b','c','d']
for i in (z):
    print(i)
#append item from one list to another list using loop
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []

for x in fruits:
  if "a" in x:
    newlist.append(x)

print(newlist) #the result will be ["apple", "banana", "mango"] bcz a is found in all items

#sort the list items
fruit = ["orange", "mango", "kiwi", "pineapple", "banana"]
fruit.sort()
print(fruit) #['banana', 'kiwi', 'mango', 'orange', 'pineapple']

num= [100, 50, 65, 82, 23]
num.sort()
print(num)

#sort list in descending order
num= [100, 50, 65, 82, 23]
num.sort(reverse = True)
print(num)

#count the items from list

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

x = fruits.count("cherry")

print(x) #result:- 2

#find index value from list items
fruits = ['apple', 'banana', 'cherry']

x = fruits.index("banana")

print(x) #index value is 1






















































Tuesday, March 14, 2023

Quiz Program With Python Programming

Create a Quiz Program With Python:-


print("Welcome To ITCI")

print("\n")
score=0
user=input("Do you want play the quiz ?  ")
if (user=='y' or user=='Y'):#user can press y or Y
    print("\nLets play!!\n")
else:
    print("See you Next Time")#if user press No then this msg display
    exit()#and program will stop
Q1=input('Q1. What is the full form of C.P.U ?\nA. Centeral Process Unit.\nB. Central Processing Unit.\nYour Answer:-')
if(Q1=='b' or Q1=='B'):
    print("Correct\n")
    score+=1
else:
    print("you are wrong\n")
Q2=input("Q1. What is the full form of A.L.U ?\nA. Airthmatic Logic Unit.\nB. Airclock Logic Unit.\nYour Answer:- ")
if(Q2=='a' or Q2=='A'):
    print("Correct\n")
    score+=1
else:
    print("you are wrong\n")
Q2=input("Q1. How many hours in a day ?\nA. 24 hours.\nB. 23 hours.\nYour Answer:- ")
if(Q2=='a' or Q2=='A'):
    print("Correct\n")
    score+=1
else:
    print("you are wrong\n")
q3=input("a for:-\nA.apple\nB.egg\nYour answer:- ")
if (q3=='a' or q3=='A'):
    print("correct")
    score+=1
else:
    print("wrong")
    
print("Your Score is",score,"out of 4.\nThanks!!")
exit()

How to Draw Square with Python.

How to Draw Square with Python :-

import turtle #first we import turtle lib
tur=turtle.Turtle() #create a storage to store turtle lib
tur.color("red")
tur.pensize(5)
tur.hideturtle() #turtle will be hide when square is drawing
'''now we create a square you can use fd or forward to move
the turtle to forward side and rt or right
for move turtle to right side.'''
tur.fd(200)
tur.rt(90)
tur.fd(200)
tur.rt(90)
tur.fd(200)
tur.rt(90)
tur.fd(200)
tur.rt(90)
turtle.done() #tell to compiler that program is done

#note: - Using for loop

import turtle #first we import turtle lib
tur=turtle.Turtle() #create a storage to store turtle lib
tur.color("red")
tur.pensize(5)
tur.hideturtle() #turtle will be hide when square is drawing
'''now we create a square you can use fd or forward to move
the turtle to forward side and rt or right
for move turtle to right side.'''
for i in range(4):
    tur.fd(200)
    tur.rt(90)
turtle.done() #tell to compiler that program is done