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

Programming Questions

more useful programming Q's

Uploaded by

mua19345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming Questions

more useful programming Q's

Uploaded by

mua19345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6) Print the second item in the fruits list.

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


print( )
7) Change the value from "apple" to "kiwi", in the fruits list.
fruits = ["apple", "banana", "cherry"]
=
8) Use the append method to add "orange" to the fruits list.
fruits = ["apple", "banana", "cherry"]

9) Use the insert method to add "lemon" as the second item in the fruits list.
fruits = ["apple", "banana", "cherry"]
"lemon")
10) Use the remove method to remove "banana" from the fruits list.
fruits = ["apple", "banana", "cherry"]

11) Print "Hello World" if a is greater than b.


a = 50
b = 10
a b
print("Hello World")
12) Print "Hello World" if a is not equal to b.
a = 50 b = 10
if(a!=b)
print("Hello World")
13) Print "Yes" if a is equal to b, otherwise print "No".
a = 50
b = 10
a b
print("Yes")

print("No")
14) Print "1" if a is equal to b, print "2" if a is greater than b, otherwise print "3".
= 50 b = 10 if(a==b):
print("1") elif(a>b):
print("2") else:
15) Print "Hello" if a is equal to b, and c is equal to d.
if a == b and c == d:
print("Hello")
16) Print "Hello" if either a is equal to b, or c is equal to d.
if a == b or c == d:
print("Hello")
17) Print i as long as i is less than 6. i = 1 while(i < 6):
print(i)
i += 1
18) Stop the loop if i is 3. i = 1 while(i < 6): if(i == 3):

i += 1
19) Loop through the items in the fruits list.
fruits = ["apple", "banana", "cherry"]
for x in fruits
print(x)
20) In the loop, when the item value is "banana", jump directly to the next item.
fruits = ["apple", "banana", "cherry"]
for x in fruits: if x ==
"banana":
21) Use the range function to loop through a code set 6 times.

22) What output is given for each block of code:

age = int(input("Enter your age: "))  user enters 13


if age >= 13:
print("You are old enough to have a paper round")
else:
print("You are too young to have a paper round")

dogs = ['willie', 'hootz']


if len(dogs) > 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = ['willie', 'hootz', 'peso', 'monty', 'juno', 'turkey']


if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog
hostel") elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!") else:
print("Okay, this is a reasonable number of dogs.")

dogs = ['willie', 'hootz', 'peso', 'monty']


if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = ['willie', 'hootz']


if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")
dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
else:
print("Okay, this is a reasonable number of dogs.")

dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
elif len(dogs) >= 1:
print("Okay, this is a reasonable number of dogs.")

dogs = []
if len(dogs) >= 5:
print("Holy mackerel, we might as well start a dog hostel!")
elif len(dogs) >= 3:
print("Wow, we have a lot of dogs here!")
elif len(dogs) >= 1:
print("Okay, this is a reasonable number of dogs.")
else:
print("I wish we had a dog here.")

dogsWeKnow = ['willie', 'hootz', 'peso', 'monty', 'juno', 'turkey']


dogsPresent = ['willie', 'hootz']
for dog in dogsPresent:
if dog in dogsWeKnow:
print("Hello, %s!" % dog.title())

23) Write code to check if number inputted by user is odd or even. Output appropriate messages to
reflect this.
24) Write a function that asks the user how old they are. Tell them if they are old enough to vote.
Then extend the program tell them how many years it is until they can retire (assume at age 65).
25) Write a function that asks the user to input a number between 1 and 20. Give a response which
indicates if the number is either within the range, too high or too low.
26) Complete the following task
Three is a Crowd
• Make a list of names that includes at least four people.
• Write an if test that prints a message about the room being crowded, if there are more than three people
in your list.
• Modify your list so that there are only two people in it. Use one of the methods for removing people
from the list, don't just redefine the list.
• Run your if test again. There should be no output this time, because there are less than three people in
the list.
• Add an else statement to your if tests. If the else statement is run, have it print a message that the room
is not very crowded.

27) Complete the following task


Six is a Mob
• Save your program from Three is a Crowd - Part 2 under a new name.
• Add some names to your list, so that there are at least six people in the list.
• Modify your tests so that
 If there are more than 5 people, a message is printed about there being a mob in the room.
 If there are 3-5 people, a message is printed about the room being crowded.
 If there are 1 or 2 people, a message is printed about the room not being crowded.
 If there are no people in the room, a message is printed abou the room being empty.

28) Write down the output of the following code:

29) What is the output of the following code:


power = 5
while power > 0:
print("You are still playing, because your power is " + power)
power = power – 1
else
print("\nOh no, your power dropped to 0! Game Over.")

30) Write a python program to print the square of all numbers from 0 to 10 31) Write a python program
to find the sum of all even numbers from 0 to 10
32) Write a python program to read three numbers (a,b,c) and check how many numbers between ‘a’ and
‘b’ are divisible by ‘c’
33) Write code for the following task:
Growing Strength
• Make a variable called strength, and set its initial value to 5.
• Print a message reporting the player's strength.
• Set up a while loop that runs until the player's strength increases to a value such as 10.
• Inside the while loop, print a message that reports the player's current strength.
• Inside the while loop, write a statement that increases the player's strength.
• Outside the while loop, print a message reporting that the player has grown too strong, and that they
have moved up to a new level of the game.

You might also like