Python Questions - Set 2 Flashcards
(27 cards)
What is the output of
~~~
2 ** 3
~~~
2 raised to power 3
8
What is the output of ,
x = 10 / 4 y = 5 / 2.0 print (x + y)
5.0
What is the output of,
x = 13 / 4 y = 13 % 4 print(x + y )
x = 3.25 y = 1 x + y = 4.25
What is the output of,
6. // 4
1.0
What is the output of the following python code if we enter 5 as input?
Num = input("Enter a Number: ") print (Num * 3 )
555
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))
str
What is the output of the following Python code,
x = 5 y = "Sally" print(str(x) + y)
5Sally
What is the output of the following python code if we enter “HelloPython” as input,
inputString = input('Enter a string: ') print(inputString*2)
HelloPythonHelloPython
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 )
15
What is the output of the following python code,
print('My age is ' + 25)
It gives anTypeError
What is the output of this Python code,
print(int("Hello"))
It gives an invalid literal error
It means, only numbers as strings can be type casted to integer
print(int('5'))
is correct
What is the output of,
~~~
print(a)
~~~
name ‘a’ is not defined
What is the output of,
print("Ha" -1)
It gives an error,
unsupported operand type for - : str & int
How to print value of character
We have to use ord() method
c = 'g' print(ord(c))
if we have a variable ,
c = 'g'
then how print this output without fstring
“The ASCII value of ‘g’ is 103 “
~~~
c = ‘g’
print(“The ASCII value of ‘” + c + “’ is”, ord(c)
```)
What is the data type of this , print(type(1_00_0000_000))
integer
How to convert octal number “ 0o123 “to decimal ?
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
How to create literals of octal number
mynum = 0o123
mynum2 = 0O1234
Capital O is also allowed
How to convert 0x123 to decimal
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()
What are the rules who create a Python variable
- Name of varuable must start with letter or underscore
- A variable name composed of uppercase and lowercase letters, digits and an underscore only
How to write comments and python
use # symbol
What is the output of,
print("Ha" * -1)
Nothing
What is the output of,
x = 'abcd' for i in range(x): print(i)
TypeError: ‘str’ object cannot be interpreted as an integer
How to use whilw loop with else statement :
num =4 guess = int(input("Guess A number : ")) while num !=guess: guess = int(input("Guess A number : ")) else: print("Congrats , you got it ")