Python Questions - Set 2 Flashcards

1
Q

What is the output of
~~~
2 ** 3
~~~

A

2 raised to power 3
8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the output of ,
~~~
x = 10 / 4
y = 5 / 2.0
print (x + y)
~~~

A

5.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the output of,
~~~
x = 13 / 4
y = 13 % 4
print(x + y )
~~~

A
x = 3.25
y = 1
x + y = 4.25
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the output of,
~~~
6. // 4
~~~

A

1.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the output of the following python code if we enter 5 as input?
~~~
Num = input(“Enter a Number: “)
print (Num * 3 )
~~~

A

555

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which method should you use in order to convert the input into a string correctly:
~~~
year_of_birth = int(input(“In what year were you born? “))
print(“You were born in “ + …(year_of_birth))
~~~

A

str

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the output of the following Python code,
~~~
x = 5
y = “Sally”
print(str(x) + y)
~~~

A

5Sally

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output of the following python code if we enter “HelloPython” as input,
~~~
inputString = input(‘Enter a string: ‘)
print(inputString*2)
~~~

A

HelloPythonHelloPython

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the output of the following python code if we enter 5 as input
~~~
Num = input(“Enter a Number: “)
Num = int(Num)
print ( Num * 3 )
~~~

A

15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of the following python code,
~~~
print(‘My age is ‘ + 25)
~~~

A

It gives anTypeError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the output of this Python code,
~~~
print(int(“Hello”))
~~~

A

It gives an invalid literal error
It means, only numbers as strings can be type casted to integer
~~~
print(int(‘5’))
~~~
is correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the output of,
~~~
print(a)
~~~

A

name ‘a’ is not defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the output of,
~~~
print(“Ha” -1)
~~~

A

It gives an error,
unsupported operand type for - : str & int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to print value of character

A

We have to use ord() method
~~~
c = ‘g’
print(ord(c))
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

if we have a variable ,
~~~
c = ‘g’
~~~
then how print this output without fstring
“The ASCII value of ‘g’ is 103 “

A

~~~
c = ‘g’
print(“The ASCII value of ‘” + c + “’ is”, ord(c)
```)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the data type of this , print(type(1_00_0000_000))

A

integer

17
Q

How to convert octal number “ 0o123 “to decimal ?

A

We have two methods,
1. Simply assighn it to variable , python automatically convert it to decimal
2. Use inbuilt function , int() , int function takes octal number as string
~~~
octal_number = “0o123” # Octal number represented as a string with “0o” prefix
decimal_number = int(octal_number, 8)
print(decimal_number) # Output will be 83
~~~

18
Q

How to create literals of octal number

A

mynum = 0o123
mynum2 = 0O1234
Capital O is also allowed

19
Q

How to convert 0x123 to decimal

A

We have two methods,
1. Simply assighn it to variable , python automatically convert it to decima
2. use int function
~~~
hexaNumber = “0X123”
decimalNumber = int(hexanumber,16)
print()
~~~

20
Q

What are the rules who create a Python variable

A
  1. Name of varuable must start with letter or underscore
  2. A variable name composed of uppercase and lowercase letters, digits and an underscore only
21
Q

How to write comments and python

A
use # symbol 
22
Q

What is the output of,

print("Ha" * -1)

A

Nothing

23
Q

What is the output of,
~~~
x = ‘abcd’
for i in range(x):
print(i)
~~~

A

TypeError: ‘str’ object cannot be interpreted as an integer

24
Q

How to use whilw loop with else statement :

A
num =4
guess = int(input("Guess A number : "))
while num !=guess:
    guess = int(input("Guess A number : "))
else:
    print("Congrats , you got it ")
25
Q

How to bypass current iteration in for loop ?

A

We have to use continue statement,
here 2 is not printed .
~~~
for i in range(5):
if i==2:
continue
print(i)
~~~
Output: 0 1 3 4

26
Q

How to stop the for loop on certain condition ?

A

We have to use break statement
~~~
for i in range(5):
if i==3:
break
print(i)
~~~
Output : 0 1 2

27
Q

What is the output of this code,
~~~
for i in range(5):
if i==3:
break
print(i)
~~~

A

the output is ,
0 1 2