0% found this document useful (0 votes)
40 views11 pages

Python Code Practice

This document contains code snippets demonstrating various array operations in Python including: 1. Looping through arrays to print or search elements 2. Initializing and populating 2D arrays 3. Retrieving specific elements from 2D arrays using indexes 4. Built-in array functions like append to add elements The code shows common array tasks like initializing, accessing, modifying, and traversing arrays using for loops. Various array types like lists, NumPy arrays, and built-in arrays are used. Examples include sorting, searching, and 2D arrays representing data like student scores.

Uploaded by

Abdullah Shahzad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
40 views11 pages

Python Code Practice

This document contains code snippets demonstrating various array operations in Python including: 1. Looping through arrays to print or search elements 2. Initializing and populating 2D arrays 3. Retrieving specific elements from 2D arrays using indexes 4. Built-in array functions like append to add elements The code shows common array tasks like initializing, accessing, modifying, and traversing arrays using for loops. Various array types like lists, NumPy arrays, and built-in arrays are used. Examples include sorting, searching, and 2D arrays representing data like student scores.

Uploaded by

Abdullah Shahzad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

// To check the num entered is positve and less then 10

n= int(input("enter a postive number"))

while n>0 and n<=10:

n= int(input("enter a postive number"))

print("its a positive num",n)

# Online Python - IDE, Editor, Compiler, Interpreter

# To check the num entered ends at 7 strating from 107 till 197

# n= int(input("enter a postive number"))

for i in range(107,200):

n = i%10

if n==7:

print(i)

# iterate through a list of number and a string

numberlist=[5,7,6,8,9]

stringlist="red"

colors=["black","pink"]

for i in numberlist:

print(i)

for i in stringlist:

print(i)

for i in colors:-+

print(i)
# To check the num entered ends at 7 strrating from 107 till 197

for i in range(107,200,10):

print(i)

# check arks and dispaly grade

marks= int(input("enter a postive number"))

if marks>=90:

print("you scored A*")

elif marks>=80 and marks<=89:

print("you scored A")

elif marks>=70 and marks<=99:

print("you scored B")

else:

print("You are ungraded")

# iterate through a list of number and a string

numberlist=[5,7,6,8,9]

stringlist="red"

colors=["black","pink","blue","orange"]

for i in numberlist:

print(i)

for i in stringlist:

print(i)

for X in colors:
print (X)

if X== "pink":

break

# Linear search

arr=[34,65,78,90,20]

item=int(input("input item to be searched"))

for i in range(0,5):

if arr[i] == item:

print("You found your desired num",item)

break

else:

print("not found")

#linear search

item=int(input("input a numb to search"))

itemarray=[34,56,78,99,100]

for i in range (len(itemarray)):

if itemarray[i]==item:

print("item found")

break

else:

print("item not in list")

#linear search without break statement

item=int(input("input a numb to search"))

flag=False

itemarray=[34,56,78,99,100]
for i in range (len(itemarray)):

if itemarray[i]==item:

index=i

flag=True

if flag==True:

print("item found at index",index)

else:

print("item not in list")

# implementation of Bubble Sort

arr = [64, 34, 25, 12, 22, 11, 90]

n = len(arr)

# optimize code, so if the array is already sorted, it doesn't need

# to go through the entire process

swapped = False

# Traverse through all array elements

for i in range(n-1):

# range(n) also work but outer loop will

# repeat one time more than needed.

# Last i elements are already in place

for j in range(n-1):

# traverse the array from 0 to n-i-1

# Swap if the element found is greater: Sorting in ascending order

# than the next element

if arr[j] > arr[j + 1]:


swapped = True

temp=arr[j]

arr[j]=arr[j+1]

arr[j+1]=temp

#arr[j], arr[j + 1] = arr[j + 1], arr[j]

else:

swapped=False

# if we haven't needed to make a single swap, we

# can just exit the main loop.

print("Sorted array is:")

for i in range(len(arr)):

print("% d" % arr[i], end=" ")

# %d . The %d operator is put where the integer is to be specified. where as % is used to concatenate
string

# 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.

# printing a string using index

mystring="HUMAIRA"

index=0

len_mystring=len(mystring)

for i in range(0,len_mystring):

mychar=mystring.lower()

mychar1=mystring[index]

print (mystring[index])
index=index+1

print (mychar)

import numpy as np

arr1 = np.array([[23,67],[78,92],[44,83]])

print(arr1)

# retriving an array element

result= arr1[2, 1]

print(result)

print(arr1[1])

arr2=[[23,45,34],[23,54,66]]

for i in arr2:

print(i)

----------------------------------------------------------------------------------------------------------------------------------------
import numpy as myArray

#from numpy import *

myArray1 = myArray.array([[27, 31, 17], [19, 67, 48],[36, 98, 29],[42, 22, 95],

[16, 35, 61], [89, 46, 47], [21, 71, 28], [16, 23, 13], [55, 11, 77]])

print(*myArray1)

# displaying a particular value from a row and a column

myarray2=myArray1[1,2]

print(myarray2)

#--------------------------------------------------------------------

# populating an 1 d array with items , or assigning items in index num

students=[]

students=[""for i in range(5)]
students[0]=60

students[1]=70

students[2]=80

students[3]=83

students[4]=90

print(students)

#-----------------------------------------------------------------------

myArray=[

["a","b"],

["c","d"]

print(myArray[0][0])

#prints a

myArray=[

[1,2,3],

[4,5,6],

[7,8,9]

print(myArray[0][0])

for row in myArray:

print(row)

#------------------------------------------------------------------------
# geeting input at run time an appending it in array

from array import*

stname=array('i',[])

namescount=int(input("Eiter numb of names u want to enter"))

print("enter names")

for j in range(namescount):

# stname.append(int(input("Enter names")))

names=(int(input()))

stname.append(names)

for j in range(namescount):

print(stname[j])

#---------------------------------------------------

#------------------------------------------------------------------------

students1=[["" for i in range(3)] for j in range(3)]

# “” or none used to initialize first value in array, it’s a format

students1[0][0]=100

students1[0][1]=90

students1[0][2]=80

students1[1][0]=70
students1[1][1]=60

students1[1][2]=50

students1[2][0]=40

students1[2][1]=30

students1[2][2]=20

for i in range(3):

for j in range(3):

print(students1[i][j])

# print()

#-------------------------------------------------------------------------------
ARRAYS PAST PAPERS

You might also like