0% found this document useful (0 votes)
12 views15 pages

Python Imp

Uploaded by

admin2 admin2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views15 pages

Python Imp

Uploaded by

admin2 admin2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

1. What is Python?

Python is an interpreted , object oriented , most popular and high level programming
language.
It was designed by “Guido Van Rassom” in 1991 and it was developed by “Python
Software Foundation”.

2. What are the key features of python?

• Easy to code : Python is a high-level programming language.

• Free and Open Source: • Object-Oriented Language:

• GUI programming support: • High-Level programming language:

• Extensible Feature:

• Python is Portable language

: • Python is Integrated language:

3. What is a collection in python?

Collections in python are basically container data types, namely lists, sets,
tuples, dictionary

• List

list is a collection of data type which is represent by [ ] list which is


orderable , indexable , mutable(changeble)

list index always start from 0


list is similar like array but in array we can contain similar element when in
list we contain similar and dis-similar element

• Tuple

tuple is a collection data type which is contain data element

tuple which is represent by ()

tuple which is immutable (can not changeble)

tuple is orderable , Indexable

• Dic
o Dictionary is a collection data type which we represent {}
o Dictionary which is unorderable , indexable ,and mutable
o Dictionary does not allow duplicate value
o It can be store members details in ‘key’ and ‘value’.
o Syntax

Dic_name = {“key”:”value”}

• Set
o Set is a collection data type which is a represent a group of unique value as a single
entity
o Set which is represent by { }
o no orderable and index & slicing not work
o set which is mutable

syntax:

o set = {"item1","item2"}
4. Explain difference between list and tuple

both are collection data type - list which is represent by [] braces when tuple are
represnt by () braces .

list is orderable, indexable but mutable when tuple is orderable ,indexable but
immutable

5. What is the namespace in python?

A Python identifier is a name used to identify a variable, function, class, module or other
object

6. What is meaning of mutable and immutable

Mutable is a change value when immutable can not change value

7. What is the lambda function in python?

A function without having any name is called lambda function

A lambda function can take any number of argument , but can only have one expression.

Syntax :

Lambda argument : expression

Examaple

X = Lambda a:a+10

Print(x(20))

8. When do we use a pass statement?

The pass statement is a null operation, it does nothing

9. What is slicing?
Slicing is a mechanism used to select a range of item from sequence
type like list , tuple , string etc…

10.How to get the last character from string in python?

The index of the last character will be the length of the string minus one

Exa.

str = "Androidpython.java"

print(str[-1])

11.Explain extend() and append() in python

• Append() :- using appeend method we can store single element in existing list
• Extend() :- using of extends method we can store more than one element in existing list (in
the form of list)

12.How to find no. of elements in a list?

Use the python built-in function len()

13.What is the meaning of negative index in python?

negative indexes is used to in python to beging slicing from end of the string.

14.Explain range() function in python ?

The range() function returns a sequence of numbers, starting from 0 by


default, and increments by 1 (by default), and stops before a specified
number.

15.What are the rules for local and global variables in python?
• Global : Variables that are created outside of a function (as in all of the examples above) are
known as global variables. Global variables can be used by everyone, both inside of functions and
outside.
• Local : A variable created inside a function belongs to the local variable of that
function, and can only be used inside that function.
16.Explain categories of functions in python
• function without parameter and function without return type
• function with parameter and function without return type
• function without parameter and function with return type
• function with parameter and function with return type

17.What is the use of format function in python?

• The format() method formats the specified value(s) and insert them
inside the string's placeholder.

• The placeholder is defined using curly brackets: {}. Read more


about the placeholders in the Placeholder section below.

• The format() method returns the formatted string.

18.Explain the use of // operator in python?


• operator : to perform specific operation at that time we need to use some specific
symbol its called operator
here, a and b operands and + is operator
1) Arithmetic operator
2) Assignment
3) relational
4) logical
5) Bitwise
6)membership

1) Arithmetic operator :-
Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication, etc. For example, (+,-,*,/,//,%,**)
num1 = 10
num2 = 20
print(num1 + num2)

2) Assignment operator : to assign something (=)


Assignment operators are used to assign values to variables. For example,

# assign 5 to x
var x = 5

shorthand operator :
when we use arithmetic and assignment same time its called shorthand operator
a=10
b=20
a += b
print(a)

3) Python Relation( Comparison ) Operators:-

Comparison operators compare two values/variables and return a Boolean


result: True or False . For example,

a=5
b =2
print (a > b) # True
Operator Meaning Example

== Is Equal To 3 == 5 gives us False

!= Not Equal To 3 != 5 gives us True

> Greater Than 3 > 5 gives us False

< Less Than 3 < 5 gives us True

>= Greater Than or Equal To 3 >= 5 give us False

<= Less Than or Equal To 3 <= 5 gives us True

4) Logical Operators :-

Logical operators are used to check whether an expression is True or False .

They are used in decision-making. For example,


a=5
b=6

print((a > 2) and (b >= 6)) # True

Here, and is the logical operator AND. Since both a>2 and b >= 6 are True , the
result is True .

Operator Example Meaning

Logical AND:
and a and b
True only if both the operands are True

Logical OR:
or a or b
True if at least one of the operands is True
Logical NOT:
not not a
True if the operand is False and vice-versa.

5) Bitwise operators :-

Bitwise operators act on operands as if they were strings of binary digits. They operate
bit by bit, hence the name.

For example, 2 is 10 in binary and 7 is 111.


In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Operator Meaning Example

| Bitwise OR x | y = 14 ( 0000 1110 )

~ Bitwise NOT ~x = -11 ( 1111 0101 )

^ Bitwise XOR x ^ y = 14 ( 0000 1110 )

>> Bitwise right shift x >> 2 = 2 ( 0000 0010 )

<< Bitwise left shift x << 2 = 40 ( 0010 1000 )

6) Membership operators :-

In Python, IN and not in are the membership operators. They are used to test
whether a value or variable is found in a sequence
(string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.

Operator Meaning Example

in True if value/variable is found in the sequence 5 in x

not in True if value/variable is not found in the sequence 5 not in x

Example 5: Membership operators in Python


x = 'Hello world'
y = {1:'a', 2:'b'}

# check if 'H' is present in x string


print('H' in x) # prints True

# check if 'hello' is present in x string


print('hello' not in x) # prints True

# check if '1' key is present in y


print(1 in y) # prints True

# check if 'a' key is present in y


print('a' in y) # prints False
Run Code

Output

True
True
True
False

19.What is the use of split function in python? :-


The split() method splits a string into a list.
Example:-

txt = "hello, my name is Peter, I am 26 years old"


x=txt.split(",")

print(x)
Output :-
['hello', 'my name is Peter', 'I am 26 years old']

20.What is a keyword and how many keywords are available in python ?


Keyword is a reserved word which has predefine meaning

Usually there are 32 keyword And which is based on respective programming language

We cannot use any reserved word as a variable name


21.How to check which version of python is installed in the system?
python –version.
22.What is a list constructor?

23.How will you capitalize the first letter of a string?


The first letter of a string can be capitalized using the capitalize() function. This method
returns a string with the first letter capitalized.

Example
cap = "python devloper"
print(cap.capitalize())
print(cap)
Output
Python devloper
python developer

24.How to sort a list?


The sort() method sorts the list ascending by default.

Example :-
num = [5,3,2,8,1,0]
num.sort()
print(num)
25.What is the function? difference between function and method
function : function is a block of code - piece of code

function which is provide reusabilities and using of function we can call same code many
times and reduce our code

there are 2 type of function

1. built-in function : which is predefine by python


e.g... print() len() range() random(), input() type()

.append(), .extend()=>method

2.User define function : which is define by user

how we can create function :


using of def keyword qwe can create function

difference between function and method

Function define outside the class function are independent

Method define inside the class and which is dependent on object

Function Method

A function is block of code that is called by A method is bock of code that is called by a
name name
It is not associated with an any object It is associated with an object
Free(free means not belong to an object or Mmber(a member of an object or class)
class)

26.What are the datatypes in python?


Which kind of data that can be store in variable.
Integer , string , Boolean , float , double all are datatype.
27.What is a list comprehension python?

28.What is a dictionary in python?


Dictionary is a collection which is unordered, changeable and indexed.
• Dictionary doesn’t allows duplicate members.
• It can be store members details in ‘key’ and ‘value’.
• It is indicated by ‘{ }’.

29. How to find all keywords in python?

and as assert break

class continue def del

elif else except False

finally for from global

if import in is

lambda None nonlocal not


or pass raise return

True try while with

yield

30.Explain the difference between del , remove and pop in list ?


Del : - delete existing list from the memmory (variable destoy)
Remove :- to remove specific element from the list
Pop :- to remove last element from the list

31.Difference between from and import in python

32.How to get a current date?

Python programming language, the datetime module contains a datetime class


used to access the computer's current date and time.

Using the datetime.now() Method


Python datetime.now() method used to display the system's current date and time.
It defines the function inside the datetime module of the Python library.

Syntax :

datetime.now()

Example :

1. import datetime
2. dt = datetime.datetime.now() # use now() method in datetime
3. print( "Display the current date of the system: ") # current date
4. print (str (dt) ) # call the dt variable to print the system date.

Output:

Display the current date of the system:


2021-02-28 18:56:52.799555
33.What is meaning of OOPs
object oriented programming system

34.Explain class and object in python


Class :-
Class is a user define datatype
Class which a contain data member and member function
Object :-
Object is a instance of class
Using of object we can access property of class

35.What is __init__ in python?


__init__ is a method or constructor in Python.
• This method is automatically called to allocate memory when a new object/instance of a
class is created.
• All classes have the __init__ method.

36.What is the main purpose of polymorphism in python?


: Poly means many and morphism means forms - one named has different different forms
its called polymorphism

e.g.
1) women
o Wife
o Daughter
o Student
o Employee
o Mother

There are two types of polymorphism:


• Method overloading: one class has multiple methods having same name but
different parameters, it is known as Method Overloading.
• Method overriding: parent and child both have same name method its called
method overriding
37.Types of inheritance in python
1.single level
2.multi level
3.multiple
4.hierachical
5.hybrid inheritance

38.Default datatype of input function


String
39.What is an exception in python?
which disturb the normal flow of program to handle a this kind of exception
its called exception handling
exception handling is possible try and except block

You might also like