Python is Object-Oriented and supports concepts such as polymorphism, operator overloading, and multiple inheritance.
Python uses indentation to identify blocks of code.
[2]
[2, 4, 6, 8]
Iteration is the repeated execution of a set of statements using loops as long as the condition is true.
Users can define custom exceptions by creating a new class that is derived from the Exception class.
Values are: block1, block2, block3
'pass' is a null statement used as a placeholder for functionality to be added later; it is not ignored by the interpreter.
The output is {'brand': 'mrcet', 'model': 'college', 'year': 2004}.
#
Yes, you can change the type of a variable after initialization, for example, x = 5 (int) and then x = 'kve' (str).
If <test expression>: Body of if statements elif <test expression>: Body of elif statements else: Body of else statements
a) Eval
Python allows you to assign a single value to several variables simultaneously, such as a = b = c = 1.
50.0
A string is a sequence of characters enclosed within quotes, and every string object is of the type 'str'.
Python uses the print statement to output variables, and variables do not need to be declared with any particular type.
The output will be 'a is smaller than the input given'.
It produces a generator object, not a tuple.
The elif statement allows checking multiple expressions for TRUE and executing a block of code as soon as one condition is true.
It prints the string two times.
KV School KV School KV School KV School KV School KV School
A set comprehension is a concise way to create a set using a single line of code, similar to list comprehensions, and is enclosed within curly brackets.
The assignment statement and the print statement.
You can assign multiple objects to multiple variables using syntax like a, b, c = 1, 2, 'kvsch'.
Comments are discarded by the Python interpreter and can be single-line (starting with #) or multi-line (using triple quotes).
You use the len() function.
You can print the first character using str[0].
It represents the starting index, stopping index, and step size for slicing a string.
It will return the characters starting from the 3rd to the 5th character, which is 'llo'.
You can use a list comprehension, for example: [(x, x**2) for x in range(6)].
You can concatenate using the + operator, e.g., str + 'TEST'.
It prints the string starting from the 3rd character, which is 'llo World!'.
while(expression): Statement(s)
The syntax is: for val in sequence1: for val in sequence2: statements.
The output is {'r', 'd'}.
You can import a module using the syntax: import <module-name>.
It receives input from the user through the keyboard.
3 is greater done
Yes, Python is an open source programming language that can be freely downloaded and installed.
A ZeroDivisionError indicates that the second argument used in a division or modulo operation was zero.
An OverflowError signifies that an arithmetic operation has exceeded the limits of the current Python runtime, typically due to excessively large float values.
An ImportError is raised when trying to import a module that does not exist, often due to a typo in the module name.
if test expression: Body of if statements else: Body of else
Python is an interactive programming language, allowing users to interact with the interpreter directly.
'rtpo'
It generates numbers up to a specified value.
You can iterate over a string using a for loop, like this: for name in college: print(name).
a is greater
The output is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].
The index indicates which character in the sequence we want to select.
This creates three variables named a, b, c and initializes them with values 5, 10, and 20 respectively.
The default value of 'start' is 0.
The output is 12.
The result is 11 because multiplication is evaluated before addition.
No, you can only iterate over it once.
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
TypeError: 'int' object is not subscriptable
int, float, complex
Python's syntax is simple and straightforward, contributing to its popularity.
Syntax errors are basic errors that arise when the Python parser cannot understand a line of code, and they are almost always fatal.
List items are ordered, changeable, and can be repeated.
A KeyError occurs when a dictionary object is accessed with a key that does not exist in the dictionary.
3 - Got a true expression value 100
ces: block1, it: block2, ece: block3
a) i,ii,iii,iv,v,vi
It initializes an empty dictionary named StudentDict.
A slice is a segment or part of a string that can be selected using the slice operator.
Operator precedence determines the order in which operators are evaluated in an expression.
-1 a is smaller Finish
A is Greater than 9
It contains the block of code that executes if the conditional expression in the if statement resolves to false.
At most only one else statement.
An IndexError is triggered when you refer to a sequence that is out of range, such as accessing an index that does not exist in a list.
10
An IndentationError occurs when there is an unexpected indent or inconsistent indentation in the code.
It returns a shallow copy of the dictionary.
The output is 10.
The output is 30.
We can’t change an existing string; we can only create a new variation.
Supports all operations for sequences, is immutable, but member objects may be mutable.
a=int(input('enter the number')) b=int(input('enter the number')) c=int(input('enter the number')) if a>b: print('a is greater') elif b>c: print('b is greater') else: print('c is greater')
The output is: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5.
Dictionaries are created using curly brackets and consist of key-value pairs.
You can create an empty tuple using X = ().
b) 30.8
The first element can be accessed with T[0] and the last element with T[-1].
A for loop is used for repeated execution of a group of statements for a desired number of times, iterating over items of lists, tuples, strings, dictionaries, and other iterable objects.
The syntax is: for <loopvariable> in <sequence>: Statement(s)
During each iteration, the loop variable holds a value from the sequence.
A way to create a new list using an existing sequence.
It returns the version information of Python, including major, minor, micro, release level, and serial.
To iterate over a sequence when the number of repetitions is known.
The loop iterates over the items in the list ['K','V','S','C','H'].
Returns true if the string contains only digits; false otherwise.
for i in range(n): rollno = input('Enter Roll No:'); name = input('Enter Name:'); physicsMarks = int(input('Enter Physics Marks:')); chemistryMarks = int(input('Enter Chemistry Marks:')); mathMarks = int(input('Enter Maths Marks:')); studentDict[rollno] = {'name': name, 'physics': physicsMarks, 'chemistry': chemistryMarks, 'math': mathMarks}
KV School KV School KV School CS DEPT
Keys are: ces, it, ece
The 'continue' statement skips the current iteration and continues with the next iteration of the loop.
b) cannot perform mathematical operation on strings
The syntax is <Name of the tuple>[index].
Tokens are the smallest identifiable units in Python, including keywords, identifiers, literals, operators, and delimiters.
Any type, but keys must be of an immutable type.
The operator for integer division is /.
The token for the remainder operation is %.
The binary left shift operator is represented by << and shifts the bits of a number to the left.
While loop and for loop.
The output is: 2, 4, 6, 8
Returns true if the string has at least 1 character and all characters are alphabetic; false otherwise.
studentDict = {}
['Hi!', 'Hi!', 'Hi!', 'Hi!']
True
It returns a new view of the dictionary's keys.
(4, 5, 66, 9)
If key is in the dictionary, it returns its value. If not, it inserts key with a value of d and returns d (defaults to None).
While Loop, For Loop, Nested For Loops
The 'break' statement terminates the loop containing it and control flows to the statement immediately after the body of the loop.
Tuples are more efficient than lists due to Python's implementation.
A syntax error or parsing error occurs when the proper structure of the language is not followed.
'puter'
'compu'
'r'
'ter'
A TypeError occurs when two unrelated types of objects are combined, such as adding an integer and a string.
It indicates the difference between every two consecutive numbers in the sequence.
Lists are used to store different types of items in a variable, allowing for the collection of data.
Strings enclosed within triple quotes that can span multiple lines.
The output is: * * * * *
L.sort(reverse=True)
Returns true if the string has at least one cased character and all cased characters are in uppercase; false otherwise.
['school', 'KVSCH!']
The pop() method removes the element at the specified position.
You can create a list using square brackets, e.g., list1=[1,2,3,'A','B',7,8,[10,11]].
a) 4
A nested for loop is when one loop is defined within another loop.
You can iterate through the list and compare each element to the item being searched for.
A Boolean expression.
Keys must be unique and of immutable types.
Examples include simple expressions like a value by itself or a variable, and more complex expressions like z=x+20.
{2: 4, 4: 16, 6: 36}
Triple double quotes (""" ) or triple single quotes (''' )
You can combine text and a variable using the '+' character, for example, print('Python is ' + x).
[3, 4, 5]
[1, 2, 3, 4, 5, 6]
D = {} while True: K = input('type a key'); V = int(input('type the value')); D[K] = V; C = input('type 'y' to add more')
for item in list: if item == given_item: print('Item found')
3
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
An ordered collection of elements, including Strings, Lists, and Tuples.
An unordered collection of any type without duplicate entries.
Returns true if the string contains only whitespace characters; false otherwise.
The clear() method removes all the elements from the list.
'school'
Returns the occurrence of a substring in another string.
A ValueError indicates a problem with the content of the object you tried to assign a value to.
A while loop keeps iterating a block of code until the desired condition is met.
An expression is a combination of values, variables, and operators that is evaluated using an assignment operator.
You can use the help() command followed by the name of any module, keyword, or topic.
'omp'
Options that use print() function with the string.
Keywords are reserved words that have special meanings, such as int, print, input, for, and while.
Identifiers cannot use keywords or operators, must start with an alphabet or underscore, cannot contain spaces, cannot start with a number, and are case sensitive.
A variable is used to store data and is an object in Python, representing a reserved memory location to store values.
The output is: 1 2 3 4 5
It indicates the end of the sequence.
It removes all items from the dictionary.
int, float, string, tuple.
It means executing the statements one by one starting from the first instruction onwards.
Returns true if the string has at least 1 cased character and all cased characters are in lowercase; false otherwise.
if <expression>: statement(s)
The output is: * * * * * * * * * * * * * * *
Python variables do not need explicit declaration; memory is reserved automatically upon assignment.
The count() method returns the number of elements with the specified value.
['b', 'c', 'd']
The insert() method adds an element at the specified position.
hello-how-areyou
It breaks out of a loop if the variable C is not equal to 'y'.
40
b) NameError
It assigns a list of student details to a key 'rollno' in the dictionary studentDict.
It outputs the version of Python currently being used, such as '3.8.0'.
'opt'
No, tuples are immutable and cannot be modified after creation.
A TypeError is raised if you try to modify a tuple.
The count() function returns the number of times a specified value occurs in a tuple.
You can use the index() function to find the position of a specified value in a tuple.
'comput'
It determines whether a given year is a leap year or not.
1. Sequential execution 2. Selection/Conditional statements 3. Iterations/loop
A sequence of characters, enclosed in single or double quotes.
They help execute statements based on whether a condition is evaluated to True or False.
It returns a new view of the dictionary's items (key, value).
10
'KVSCH'
a = 100
The reverse() method reverses the order of the list.
b. Mutability
2, 3, 5, 7
A statement is an instruction that the Python interpreter can execute.
90.0
The assignment operator assigns a value to a variable and evaluates expressions.
The character set includes letters (A-Z, a-z), digits (0-9), special symbols (+, -, /, %, **, *, [], {}, #, $), white spaces (blank space, tabs, carriage return, newline, form feed), and all ASCII and UNICODE characters.
Python supports Arithmetic Operators, Relational Operators, Logical Operators, and more.
The token for addition is +.
Sequence, Selection, and Iteration.
Immutable types cannot be modified, while mutable types can be modified.
L = [x for x in range(m, n + 1) if x % 2 != 0]
The output is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Returns true if the string has at least 1 character and all characters are alphanumeric; false otherwise.
L = [x**2 for x in range(11)]
[1, 2, 3, 4, 5, 6]
[25, 36, 49, 64, 81]
[1, 9, 25, 49, 81]
The block of statement(s) inside the if statement is executed.
(5, 5)
Replaces all occurrences of old in the string with new, or at most max occurrences if max is given.
It returns a new view of the dictionary's values.
It removes or pops the specific item from the dictionary.
The remove() method removes the first item with the specified value.
a. total
School 1 is K, School 2 is V, School 3 is S, School 4 is C, School 5 is H
The output is: 1 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
A Python module is a program file that contains Python code, including functions, classes, or variables, and is saved with the .py extension.
It displays something on the screen/monitor.
It returns the index of the first occurrence of a specified value in the tuple.
It allows the program to execute one block of code if a condition is true and another block if it is false.
x is assigned 13 because multiplication has higher precedence than addition.
Dynamic typing, built-in types and tools, library utilities, third-party utilities (e.g., NumPy, SciPy), and automatic memory management.
Python runs on virtually every major platform, and programs will run the same way as long as a compatible Python interpreter is installed.
Python does not require an intermediate compiler, programs are compiled to byte code automatically, and its structure and syntax are intuitive.
Python is processed at runtime by the Python interpreter.
(5, 5)
30
'retupmoc'
It concatenates the strings.
Items in a list are accessed using indexes.
0,1,2,3,4,5,6,7,8
[1]
The loop will execute 10 times, printing the value of A from 0 to 9.
It updates the dictionary with the key/value pairs from other, overwriting existing keys.
c = 'John'
It deletes a particular item from the dictionary.
Write Python code to remove an element as entered by the user from the list, L.
b) delete 2nd and 3rd element from the list
The output will be 'B is Greater than A'.
The output is 'Python is awesome'.
Yes, there can be an arbitrary number of elif statements following an if.
['a', 'b', 'c', 'd', 'e', 'f', 'g']
They perform concatenation and repetition, resulting in a new list.
A way to create a new list using an existing list with the syntax: NewList = [ expression for variable in iterableobject if condition == True ]
A special type with an unidentified value or absence of value.
[1, 2, 3, 4, 5, 6, 7, 8]
['b', 'c', 'd']
[2, 3, 4, 5, 6, 7, 8, 9, 10]
The loop will execute infinitely.
The assignment operator (=).
b = 1000.0
Finds the index of the first occurrence of a substring in another string.
Determines if the string or a substring starts with a specified substring; returns true if so, false otherwise.
hello-how-are-you
NameError: name 'D' is not defined
* * * * * *
A run-time error happens when Python understands the code but encounters trouble when executing it.
It causes repetition of the characters the specified number of times.
Dictionaries, which consist of key-value pairs where keys are used to access values.
Returns true if the string is properly 'titlecased'; false otherwise.
TypeError
The extend() method adds the elements of a list (or any iterable) to the end of the current list.
L.sort(reverse=True)
The output will be [1, 2, 3, 'A', 'B', 7, 8, [10, 11]].
c) pop()
To represent True or False values, typically used in comparisons.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
infinite loop. Condition / test expression is always True.
0,1,2,3,4,5,6,7,8,9
A collection that is ordered and unchangeable, created by enclosing items within round brackets.
It removes the item with key and returns its value or d if key is not found. Raises KeyError if d is not provided and key is not found.
[1,2,3,4,5,6,7,8]
hello how are you----
It returns a new dictionary with keys from seq and value equal to v (defaults to None).
It returns the value of key. If key does not exist, it returns d (defaults to None).
It means that the contents of a list can be changed after it is created.
Mutable types can be changed after creation, while immutable types cannot.
It removes and returns an arbitrary item (key, value). Raises KeyError if the dictionary is empty.
d = '''Hello world good morning'''
D = {'A': 20, 'B': 30, 'C': 40, 'D': 50}
Returns true if the string contains only numeric characters; false otherwise.
The append() method adds an element at the end of the list.
When the contents shouldn't change, to prevent items from being accidentally added, changed, or deleted.
[1,2,3,4,5,6,7,8,9]
1 1
The syntax is <dictionary> = { expression for <variable> in sequence }.
d) Explicit type casting
1 1
The first set of code after the end of the if statement(s) is executed.
The copy() method returns a copy of the list.
The output will be x [], indicating an empty list.
hello how are you
It indicates the increment between each number in the sequence.
Splits the string according to a delimiter (space if not provided) and returns a list of substrings.
Converts lowercase letters in a string to uppercase and vice versa.
The sort() method sorts the list.
c) adds multiple elements at last
The index() method returns the index of the first element with the specified value.
(4, 5, 66, 9)
a) Python only evaluates the second argument if the first one is False
You use the len() method to get the length of the dictionary.