0% found this document useful (0 votes)
2 views13 pages

Key Features and Operators in Python

The document discusses key features of Python, highlighting its dynamic typing, ease of learning, and large standard library. It also explains predefined keywords, mutable vs. immutable objects, and various types of operators in Python, providing examples for each. The content serves as a comprehensive overview for beginners looking to understand Python programming concepts.
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)
2 views13 pages

Key Features and Operators in Python

The document discusses key features of Python, highlighting its dynamic typing, ease of learning, and large standard library. It also explains predefined keywords, mutable vs. immutable objects, and various types of operators in Python, providing examples for each. The content serves as a comprehensive overview for beginners looking to understand Python programming concepts.
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

6/25/25, 9:08 PM python basic questions 1

Python { Basic Question }


Q1. Explain the key features of python that make it a popular choice programming.

Python is a dynamic,high-level,free open source programming language. In this we don't


need to declare the type of variable because it is dynamically typed [Link] we
can see the type of variable it is,if we want to . For example -

In [2]: s = 3.4
s

Out[2]: 3.4

In [4]: j = "sofia"
j

Out[4]: 'sofia'

In [5]: type(s)

Out[5]: float

In [6]: type(j)

Out[6]: str

In [8]: #Features of python-

[Link] and open source - Python language is freely available at the official websites and
you can also download [Link] it is an open source i.e available publically.

[Link] to code - it is very easy to learn the language as comapred to other languages
like- C++ , java , javascript etc.

[Link] - it refers to the space at the beginning of a code line .This makes the code
more readable .

[Link] standard library - it has a large standard library that provides a set of modules
,function like different notebook to perform our code . This enhances its capacities,
making it a top choice for web development, data analysis, machine learning and many
more.

[Link] error - we can easily analyse error.

[Link] - same code can be used on different [Link] is no need to write same
program multiple times for several platforms.

[Link] - python need to use only a few lines of code to perform complex [Link]
example

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 1/13


6/25/25, 9:08 PM python basic questions 1

In [9]: print("hey viewers")

hey viewers

[Link] the role of predefined keywords in python and provide examples of how
they are used in a program .

• A keyword refers to a predefined word that python reserves for working programs that
have a specific meaning .You cannot use it anywhere else.

•Python has 33 keywords.

•Keywords are case-sensitive, except for True, None and False, which must be lowercase.

•Keywords cannot be used as identifiers, names for variables, functions, or classes.​

In [11]: #List of Some Predefined Keywords in Python

import keyword
print([Link])

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'clas
s', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass',
'raise', 'return', 'try', 'while', 'with', 'yield']

In [12]: #Here some of them are explained below with an example :-

[Link]- NoneType class that is used to define a null variable or an object in Python.
Example-

In [14]: my_age = None


my_age

In [15]: type(my_age)

Out[15]: NoneType

[Link] Flow Keywords

Keywords like if, else, elif, while, for, break, and continue are used to control the flow of
execution in your program. Example-

In [17]: p = 10

In [18]: if p > 5:
print("x is greater than s")
elif p == 5:
print("x is equal to 5")
else:
print("x is less then 5")

x is greater than s

[Link] and Class Definition Keywords

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 2/13


6/25/25, 9:08 PM python basic questions 1

def is used to define a function, and class is used to define a class. Example -

In [19]: def welcome():


print("hey viewers!!")
print("welcome back to my channel")

welcome()

hey viewers!!
welcome back to my channel

In [20]: class DemoSession:


a = 2

def sumvalue(self):
print(20+30)

demoobject=DemoSession()
[Link]();

50

[Link] and Anonymous Functions

lambda is used to create small anonymous functions. Example-

In [21]: exp_o = lambda x: x**5


print(exp_o(3))

243

In [22]: _add = lambda m, n : m + n


print(_add(10 ,24.7))

34.7

[Link] Scope Keywords-

'Global' and 'nonlocal' are used to work with variables in different scopes. Example-

In [24]: x = 10 #Global variable

def my_funt_n():
y = 7.8 # local variable

my_funt_n()
print(x)
#print(y) this will cause error as 'y' is a local variable.

10

[Link] Operators in Python

In [27]: #They're of two type:

➾ 'and' Operator - The 'and' operator returns True if both conditions on either side are
True. If either or both conditions are False, it returns False. Example-

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 3/13


6/25/25, 9:08 PM python basic questions 1

In [28]: x = 13
y = 23.4

#both conditions need to be true.


if x > 7.8 and y > 21:
print("both conditions are true")
else:
print("atleast one condition is false")

both conditions are true

➾ 'or' operator - The 'or' operator returns True if at least one of the conditions on either
side is True. It only returns False if both conditions are False.

In [29]: p = 56.9
q = -40

#atleast one condition need o be true.


if p < 28 or q > -40.5:
print ("alleast one condition is true.")
else:
print ("both the condtions are true.")

alleast one condition is true.

In [30]: # if both are used together i.e


a = 3
b = 10
c = 7

if (a > 1.5 and c < 4) or b < 6:


print("conditions are met")
else:
print("conditions are not met.")
#since c < 4 and b < 6 are false.

conditions are not met.

Q3. Compare and contrast mutable and immutable objects in python with examples.

In Python, objects can be categorized as mutable or immutable based on whether their


state or value can be changed after they are created.

✦ Immutable Objects

Immutable objects are those whose state or value cannot be modified once they are
created. Attempting to change an immutable object results in the creation of a new
object rather than altering the existing one.

Examples of Immutable Objects:- Numbers (e.g., float) , Strings (str)

In [31]: s = "pwskills"
s[1]

Out[31]: 'w'

s[1] = "t" #this will show error as item assigned is string.

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 4/13


6/25/25, 9:08 PM python basic questions 1

In [33]: x = "BLACK"

x = x + "_BEAUTY"
print(x)

BLACK_BEAUTY

✦ Mutable Objects

Mutable objects are those whose state or value can be modified after they are created.
Changes made to a mutable object affect the original object.

Examples of Mutable Objects:- Lists (list) , Dictionaries (dict) , Sets (set)

In [34]: list_cont = [ 2.59 , True , 'Muskan', 100 , 66.03 , 3 +7j ]

list_cont[5] = 'SHINCHAn'
print(list_cont)

[2.59, True, 'Muskan', 100, 66.03, 'SHINCHAn']

In [35]: list_cont[2]

Out[35]: 'Muskan'

In [36]: type(list_cont)

Out[36]: list

In [37]: list_cont[0] = 48
print(list_cont)

[48, True, 'Muskan', 100, 66.03, 'SHINCHAn']

Comparison

1. Modification:

• Immutable Objects: Cannot be changed after creation. Any modification results in a


new object.

•Mutable Objects: Can be changed in place. Modifications affect the existing object.

2. Memory Usage:

• Immutable Objects: Often optimized for performance, since their state does not
change. Python may reuse existing immutable objects to save memory.

•Mutable Objects: May consume more memory if modifications create new objects or
lead to additional data structures.

3. Hashability:

• Immutable Objects: Generally hashable and can be used as keys in dictionaries or


elements in sets because their value does not change.

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 5/13


6/25/25, 9:08 PM python basic questions 1

• Mutable Objects: Generally not hashable and cannot be used as dictionary keys or set
elements because their value can change.

Q4. Discuss the different types of operators in python and provide examples of how they are used. In Python, operators are
special symbols or keywords that perform operations on values or variables. Python provides a variety of operators, each
serving a different purpose. Different types of operators are-1. 1. Arithmetic Operators [Link] Operators [Link]
Operators [Link] Operators [Link] Operators [Link] Operators [Link] Operators Operators 🔸Arithmetic
Operators - These operators are used to perform basic arithmetic operations.

In [38]: d = 0.0113
e = 51.78
print(d + e) #'+'addition operator

51.7913

In [40]: p = 12
q = 01.96
print(p * q) #'*'multiplication operator

23.52

In [41]: x = 17
y = 5
print(17 // 5) #'//'floor division operator(show quotient)

In [42]: a = 100
b = 16
print(a % b) #'%'modulus operator (returns the remainder of the division)

In [43]: s = 7
t = 3
print(s**t) #'**'exponential (first operand to the power to the second)

343

🔸Comparison Operators - These operators are used to compare two values.

In [44]: b = 58
c = 30
print(b == c) #equal to(==)

False

In [45]: m = 17
n = 8
print(m != n) #not equal('!=')

True

In [46]: p = 42
q = 6.67
print(p >= q) #greater than or equal to( >=)

True

In [47]: e = 10
f = 15.7
print(e <= f) #less than or equal to(<=)

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 6/13


6/25/25, 9:08 PM python basic questions 1

True

🔸Logical Operators - These operators are used to combine conditional statements.

In [48]: y = 6.7
z = 12
print(y > 6 and z < 49) #'and' operator

True

🔸Assignment Operators - These operators are used to assign values to variables.

In [49]: r = 8.9
r += 1.55
print(r) #(+=): Adds the right operand to the left operand and assigns the resul

10.450000000000001

In [50]: x = 8.09
x -= 4
print(x) # (-=): Subtracts the right operand from the left operand and assigns

4.09

In [51]: a = 3.79
a *= 8
print(a) # (*=): Multiplies the left operand by the right operand and assigns t

30.32

In [52]: c = 19
c %= 5
print(c) # (%=): Applies the modulus operator and assigns the result to the lef

🔸Bitwise Operators - These operators perform bit-level operations.

In [54]: #'and'() operator


a = 18 & 7
print(a)

In [55]: u = 15 & 15
print(u)

15

In [56]: e = -9 & 6
print(e)

In [57]: f = -5 & -3
print(f)

-7

In [58]: #'or'(|)operator
b = 7 | 5
print(b)

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 7/13


6/25/25, 9:08 PM python basic questions 1

In [59]: a = 5 | 5
print(a)

In [60]: #'~'(not) operator - this convert the value into negative value by finding its 1
g = ~5
print(g)

-6

In [61]: s = ~(-9)
print(s)

In [62]: #'^'(XOR) operator


p = 5^3
print(p)

In [63]: q = 8^8
print(q)

In [64]: #Shift operator


#1. Left shift operator (<<)
#2. Right shift operator (>>)

In [65]: # (<<) left shift operator - shifts the bits to the left.
a = 34 << 4
print(a)

544

In [66]: z = 5 << 3
print(z)

40

In [67]: # (>>) Right shift operator - shifts the bits to the right.
x = 13 >> 3
print(x)

In [68]: k = 24 >> 3
print(k)

🔸Assignment operator -it is used to assign values to variable.


#operators - # = assignment operator # += addition assignment operator # -= subtraction assignment operator # *=
multiplication assignment operator # /= division assignment operator # %= remainder assignment operator # **= exponential
assignment operator

In [69]: a = -6
a -= 8
print(a)

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 8/13


6/25/25, 9:08 PM python basic questions 1

-14

In [70]: b = 45.7
b /= 4
print(b)

11.425

In [71]: k = 154
k %= 7.6
print(k)

2.000000000000007

In [72]: s = 7
s **= 3
print(s)

343

🔸Membership Operator - detect whether input exist in the given location or not by
using in / not in.

In [73]: string_1 = "keep it up"


print('z' in string_1)

False

In [74]: lis_t = [156 , 'oh_ha_ni', 56.7 , 3.77]


print('46' not in lis_t)

True

🔸Identity Operator - this operator is used to check whether two objects are of same
type or not and they share memory location or [Link] always return "True or False" as
output.

In [76]: x = 12
y = 33
print(x is y)

False

In [77]: a = 15.8
b = 79
c = a
print(c is a)
print(a is b)

True
False

Q5. Explain the concept of type casting in python with example.

Type casting in Python is the process of converting one data type into another. Python
supports several built-in functions to perform type casting, allowing you to convert
values between different data types as needed.

Here are some common type casting functions in Python:

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 9/13


6/25/25, 9:08 PM python basic questions 1

int(): Converts a value to an integer.

float(): Converts a value to a float.

str(): Converts a value to a string.

bool(): Converts a value to a boolean.

In [ ]: #convert sstring into integer


a = 'paris'
b = int(a)
print(b) # this will show an error as string(characters) cannot be changed.

In [79]: p = '8'
q = int(p)
print(q) #here string isn't containing characters."

In [80]: #convert integer into float


k = 4
p = float(k)
print(p)

4.0

In [81]: #convert float into boolean


e = 6.5
f = bool(e)
print(f)

True

In [82]: u = 6.9
v = bool(u)
print(v)

True

In [83]: #convert boolean into integer


x = 1
y = int(x)
print(y)

In [84]: a = 0
b = int(a)
print(b)

In [85]: #convert boolwan into float


s = 1
t = float(s)
print(t)

1.0

In [86]: #convert boolean into string


a = 0

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 10/13


6/25/25, 9:08 PM python basic questions 1

b = str(a)
print(b)

Type casting in Python can be categorized into several types based on the conversion
direction between data types. Here’s a detailed explanation with examples:

1.

Implicit Type Casting (Automatic Type Conversion) - Implicit type casting, or automatic
type conversion, occurs when Python automatically converts one data type to another
during an operation. This typically happens when mixing types in an expression. Examplle
-

In [87]: a =7
type(a)

Out[87]: int

In [88]: x = 8 #integer
y = 0.5 # float
x + y #here in output implicity converted to a float to match the type of y

Out[88]: 8.5

2. Explicit Type Casting (Manual Type Conversion)

Explicit type casting involves manually converting a value from one type to another using
built-in functions like int(), float(), str(), and bool(). This is done when you need to enforce
a specific type for a [Link]-

In [89]: a = 9.45
b = int(a)
print(b)

In [90]: x = "321"
y = int(x)
print(y)

321

In [91]: b = '78'
c = float(b)
print(c)

78.0

[Link] do conditional statements work in python? Illustrate with example.

Conditional statements in Python allow you to execute different blocks of code based on
certain conditions.

Python provides several conditional statement1. s:

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 11/13


6/25/25, 9:08 PM python basic questions 1

if Stat2. ement if-else Sta3. tement if-elif-else Statement

In [92]: #if condition - to execute code if condition ned to be true.


a = 10
b = 20
if b < a:
print ("A is greater than B") #here as the if condition is false ,that's why

In [93]: a = 40.5
b = 23.9
if a > b:
print('B is lesser than A') #Here the result is true, that's why it showed r

B is lesser than A

In [94]: # if-else condition


age = 24
if age >= 18:
print('you are an adult')
else:
print("you're a minor" )

you are an adult

In [95]: x = 5.0
y = 9
if y > x:
print('y and x are equal')
else:
print('y is greater than x')

y and x are equal

In [96]: # if-elif-else condition


a = 39
b = 22
c = 10
if (a > b) ^ (a > c):
print('a is greater than b')
elif (b > a) ^ (b > c):
print("b is greater than c")
else:
print('c is the greatest')

b is greater than c

In [97]: temp_ = 35
if temp_ < 5:
print("it's freezing outside")
elif 0 <= temp_ <= 22:
print("it's cold")
elif 22 <= temp_ <= 37:
print("it's hot!!!")
else:
print("it's extremely hot")

it's hot!!!

Q7. Describe the different types of loops in python and their use cases with example .

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 12/13


6/25/25, 9:08 PM python basic questions 1

In Python, loops are used to execute a block of code repeatedly. There are three primary
types of loops: for loops, while loops, and nested loops.

[Link] Loop

The while loop is used to execute a block of code as long as a specified condition is
[Link] -

In [98]: #while loop with 'else'


count = 19

while(count <=20):
count += 2
print("great job")

else:
print("loop has been finished successfully")

great job
loop has been finished successfully

[Link] Loop

The for loop in Python is used to iterate over a sequence (like a list, tuple, string, or
range) and execute a block of code for each element in the sequence.

In [100… #for loop


fruits = ['apple','banana','papaya','pineapple']
for fruit in fruits:
print(fruits)

['apple', 'banana', 'papaya', 'pineapple']


['apple', 'banana', 'papaya', 'pineapple']
['apple', 'banana', 'papaya', 'pineapple']
['apple', 'banana', 'papaya', 'pineapple']

In [101… l = [23.4, 111, 'life', 'bill', 700]


for i in l:
print(i , end =" ")

23.4 111 life bill 700

In [102… for i in l:
if i == 'bill':
continue
print(i)
else:
print("check your list")

23.4
111
life
700
check your list

localhost:8890/lab/tree/python basic questions [Link]#Python-Basic-Question---1 13/13

You might also like