[go: up one dir, main page]

0% found this document useful (0 votes)
65 views6 pages

Copy

The document contains answers to various questions about Python programming concepts like variables, data types, strings, numbers, operators, conditional statements, loops, functions and more. It also includes sample code snippets to demonstrate max of two numbers, fizzbuzz, speed check, even/odd number printer, sum of multiples, star pattern printer and prime number checker programs.

Uploaded by

Kishan Pandey
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)
65 views6 pages

Copy

The document contains answers to various questions about Python programming concepts like variables, data types, strings, numbers, operators, conditional statements, loops, functions and more. It also includes sample code snippets to demonstrate max of two numbers, fizzbuzz, speed check, even/odd number printer, sum of multiples, star pattern printer and prime number checker programs.

Uploaded by

Kishan Pandey
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/ 6

ANSWERS-

1 What is a variable?
Ans variable are the name used to store values at computer memory locations in a program.

2 primitive data types


Ans primitive data types are the simplest form if representing data.
We have the following primitive data types in python
Integers
Float
Strings
boolean

3 When should we use “”” (tripe quotes) to define strings?


Ans when we have a very long string and we want it to be in multi-lines

4 Assuming (name = “John Smith”), what does name[1] return?


Ans it will return ‘o’

5 What about name[-2]?


Ans it will return ‘t’

6 What about name[1:-1]?


Ans it will return ‘ohn Smit’

7 How to get the length of name?

Ans len(name)

8 What are the escape sequences in Python?


Ans escape sequences are usually help us to introduce a special character in string.

9 What is the result of f“{2+2}+{10%3}”?

Ans “4+1”

10 Given (name = “john smith”), what will name.title() return?


Ans it will return ‘John Smith’

11 What does name.strip() do?


Ans it will trim the spaces from starting and ending of the string.

12 What will name.find(“Smith”) return?


Ans it will return 5 i.e the index from which the given string is starting

13 What will be the value of name after we call name.replace(“j”, “k”)?


Ans it will be same as earlier as string is immutable

14 How can we check to see if name contains “John”?


Ans val = "John" in "John Smith"
print(val)

it will return true if the string we want to find is there in the given string

15 What are the 3 types of numbers in Python?


Ans the following are the 3 types of numbers in python
Integers
Float
Complex

1 What is the difference between 10 / 3 and 10 // 3?


Ans the 10/3 will give the float value i.e 3.333333335
and 10//3 will give the floor value i.e 3

2 What is the result of 10 ** 3?


Ans this expression will return 10^3

3 Given (x = 1), what will be the value of after we run (x += 2)?


Ans value of x will be 3

4 How can we round a number?


Ans we have a function that is round(number,upto decimal places)

5 What is the result of float(1)?


Ans it will return 1.0

6 What is the result of bool(“False”)?


Ans will return True /// how ?
7 What are the falsy values in Python?

8 What is the result of 10 == “10”?


Ans this will return false

9 What is the result of “bag” > “apple”?


Ans this will return True

10 What is the result of not(True or False)?

Ans
// did not understand the expression

11 Under what circumstances does the expression 18 <= age < 65 evaluate to True ?
Ans the age should be in range [18,65)

12 What does range(1,10,2) return?


Ans 1 2 4 6 8

Coding Exercises

1 .Write a python program that prints the maximum of two numbers.


Ans
number1 = int(input("enter number 1 "))
number2 = int(input("enter number 2 "))
if(number1 > number2):
print("{} is greater than {}".format(number1,number2))
elif(number1 < number2):
print("{} is greater than {}".format(number2,number1))
else:
print("both {} and {} are equal".format(number1,number2))

2 Write a python called fizz_buzz that takes a number.


If the number is divisible by 3, it should return “Fizz”.
If it is divisible by 5, it should return “Buzz”.
If it is divisible by both 3 and 5, it should return “FizzBuzz”.
Otherwise, it should print the same number.

Ans
fizzyNumber = int(input("enter the fizzy number "))
if(fizzyNumber % 3 == 0 and fizzyNumber % 5 ==0):
print("FizzBuzz")
elif(fizzyNumber % 3 == 0):
print("Fizz")
elif(fizzyNumber % 5 == 0):
print("Buzz")
else:
print(fizzyNumber)

3 Write a python program for checking the speed of drivers. This function should have one
parameter: speed.
If speed is less than 70, it should print “Ok”.
Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point
and print the total number of demerit points. For example, if the speed is 80, it should print:
“Points: 2”.
If the driver gets more than 12 points, the code should print: “License suspended”
def speedCheck(speed):
if(speed <= 70):
print("Ok")
else:
extraSpeed = speed - 70
demeritPoints = extraSpeed//5
if(demeritPoints == 12):
print("License suspended")
else:
print("Points: {}".format(demeritPoints))
speed = int(input("enter speed "))
speedCheck(speed)

4 Write a python program called showNumbers that takes an input called limit. It should print all
the numbers between 0 and limit with a label to identify the even and odd numbers. For
example, if the limit is 3, it should print:
0 EVEN
1 ODD
2 EVEN
3 ODD

Ans

limit = int(input("enter the limit "))


for i in range(0,limit+1):
if(i%2 == 0):
print("{} EVEN".format(i),end="\n")
else:
print("{} ODD".format(i),end="\n")

5 Write a python program that returns the sum of multiples of 3 and 5 between 0 and limit
(parameter). For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.

Ans
limit = int(input("enter limit "))
sumOfMultipleOf3And5=0
for i in range(3,limit+1):
if(i%3 ==0 or i%5==0):
sumOfMultipleOf3And5 = sumOfMultipleOf3And5 + i
print(sumOfMultipleOf3And5)

6 Write a python program called show_stars(rows). If rows is 5, it should print the following:
*
**
***
****
*****

Ans
rows = 5
for i in range(0,rows):
for k in range(0,i+1):
print("*",end="")
print(end="\n")

7 Write a python that prints all the prime numbers between 0 and limit where limit is a
parameter.

limit = int(input("enter limit "))


for number in range(2,limit+1):
flag=0
for i in range(2,((number//2)+1)):
if(number%i == 0):
flag=1
break;
if(flag == 0):
print(number,end=" ")

You might also like