Python Questions - Set 1 Flashcards
What is the difference between method and function ?
Method Associates with class object.
While function works its own
How to print Zen of python
import this
In which language Python is written
Python is written in C language
Python interpreter written in which Language
Python interpreter is written in C language
How to interact Python code with Java code
We have to use Jython language
Python is interpreter based or compiler based
Python is interpreter based
What is interpreter
An interpreter is a program that directly executes the instructions in a high-level language, without converting it into machine code.
How to convert python code to C code
We have to use Cython a language
Which programming language is used by developers of python
Rpython,
Its used by programmers that develop python itself , since it doesn’t need an interpreter
How to get this output without fsrtring ,
if n= 25
My age is 25
n = 25
print(“My age is “,n)
How to write long multiple line code on python console
print(“Hello”);\
print(“To”);\
print(“python”)
What are keyword arguments of print function, give example ?
end=” “
sep=” “
print(“My name is Sundy “, “Dog Khan “,sep=”<> “,end=”#”)
What is the special character in Python
Backlash is an special character in python
What are literals
Constant values or fixed value ,
Values that never change during the whole execution of program
What is the output of this code,
give reasons in support of ur answer ?
mynum = 0o123
type(mynum)
printy(mynum)
int
83
python assumes octal numbers as integers ,
so the code assigns the decimal value 83 to the variable myoctal, and then it prints the value of myoctal using the print function.
How to save octal number in variable
myOctalNumber = 0o123
How to save hexadecimal number in variable
myhexaNumber = 0x123
How to print object class of identifiers ?
We have to use type method()
a = 10
type(a)
How to write below decimal number in short or scientific notation - 0.000005
5e-6 in decimal form = 0.000005
How to write Boolean values in Python
True , False
Hello to “Python”
How to create this type of string constant
We have to use special character of python ,
the backslash character , or we use single quotes
Conversion
mystr = “Hello to "Python"”
mystr = ‘Hello to “Python”’ ‘
Create an constant of octal number number , and print it to decimal
myoctal= 0o123 # it converted to base 10 first & then assighned to variable
print(“Decimal Equivalent is “,myoctal)
What are the unary operators in Python
Operators which works on single operands.
Unary plus +
Unary minus -
Bitwise not ~
+x , -x ~x
What are membership, identity & comparison operators in python ?
Membership Operator - not , not in
Identity operator - is , is not
Comparison operators - == , <= , >= , !=
How to write logical operators in Python ,
and , or , not
print(1 and 0)
print(1 or 0)
print(not 1)
What is the output of print(not 22)
False
What is the output of print(22 or 0)
22
What is the output of print(not b)
name ‘b’ is not defined
What is the Presidence of operators in Python
In python we have eight levels of Presidence
- Parenthesis ()
- Exponent **
- Unary Operators -x , +x , ~x
- Multipication & Division Operators - * / % //
- Addition & Substraction + , -
- Bitwise operators - shift, AND, XOR, OR
- Comparison , Identity , Membership Operators
- Logical Operators
What are the division operators in Python
/ Division
% Modulus
// Floor Division
What are the bitwise operators in Python
«_space;Bitwise Left »_space; Right Shift
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
What is the presidence of bitwise operators
- Bitwise Shift
- Bitwise AND
- Bitwise XOR
- Bitwise OR
What is the presidence of Logical Operators
- not
- and
- or
What is the output of print(1 and 200) , give reason in support of ur answer
in a logical AND operation like “1 and 200,” the result would be the second operand, which is 200. So, in this context, the output is indeed 200
How to write bitwise not operator
~x
How to write Bitwise AND
&
How to write Bitwise OR
|
How to write Bitwise XOR
How to write Bitwise Shift
> > Right Shift
Left Shift «
How to write Exponent operator
**
What is the output of this code ,
print(5 % 2)
The output is 1
What is the output of this code ,
print(5 // 2)
The output is 2
What is the output of this code ,
print(5 / 2)
The output is 2.5
What is the output of this code ?
~~~
print(‘My age is ‘ + 25)
~~~
It shows an error
TypeError: can only concatenate str (not “int”) to str
The Correct way is .
~~~
print(‘My age is ‘ ,25)
~~~
How to remove \n default behaviour of print function
We have to use keyword argument, to remove \n default behaviour of print function
~~~
print(“Hello to”,end=” “)
print(“Python “)
~~~
How to print symbol between two strings in single print statement
We have to use keyword argument,
~~~
print(“Hello to”,”Python”,sep=”#”)
~~~
What is the output of -6 / 4 ?
-1.5
What is the output of 6. // 4
1.0
Floor Division rounds down to the nearest integer,The actual result of 6 / 4 is 1.5 , but floor division rounds down to 1 .
What is the output of 6 // -4
Give reason in support of ur answer
-2
In Python, when you use the double forward slash // operator for division, it performs floor division, which means it rounds down the result to the nearest integer. When you perform the operation 6 // -4, it will give you the result of -2, because -1.5 (the exact division result) is rounded down to the nearest integer, which is -2.
What is the output of
~~~
10 - 6 ** 2 / 9 * 10 + 1
~~~
-39