4
4
float - holds floating decimal points and it's accurate up to 15 decimal places.
Number Systems:
Python Keywords:
Comparison operators:
Logical operators:
Bitwise operators:
Create a List:
Ex:
List = [1,2,3,5]
*A list can store elements of different types (integer, float, string, etc.), store duplicate elements
List Methods:
Python Tuple:
A tuple in Python is similar to a list. The difference between the two is that we cannot change the
elements of a tuple once it is assigned whereas we can change the elements of a list.
A tuple is created by placing all the items (elements) inside parentheses (), separated by commas.
The parentheses are optional; however, it is a good practice to use them.
We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar)
data types. Since tuples are immutable, iterating through a tuple is faster than with a list. So, there is
a slight performance boost. Tuples that contain immutable elements can be used as a key for a
dictionary. With lists, this is not possible. If you have data that doesn't change, implementing it as
tuple will guarantee that it remains write-protected.
Python Strings:
A set is a collection of unique data. That is, elements of a set cannot be duplicate.
In Python, we create sets by placing all the elements inside curly braces {}, separated by comma.
A set can have any number of items and they may be of different types (integer, float, tuple, string
etc.).
But a set cannot have mutable elements like lists, sets or dictionaries as its elements.
* When you run a code, you might get output in a different order. This is because the set has no
particular order.
We create dictionaries by placing key:value pairs inside curly brackets {}, separated by commas.
*Dictionary keys must be immutable, such as tuples, strings, integers, etc. We cannot use mutable
(changeable) objects such as lists as keys.
Python Functions:
Standard library functions - These are built-in functions in Python that are available to use.
User-defined functions - We can create our own functions based on our requirements.
Components used for writing functions: The def keyword, along with the function name is used to
define the function.
The function block is started with the colon (:), and block statements must be at the same
indentation.
The return statement is used to return the value. A function can have only one return.
When the function is called, the control of the program goes to the function definition.
The control of the program jumps to the next statement after the function call.
After creating a function we can call it by using the name of the function followed by parenthesis
containing parameters of that particular function.
A Python function may or may not return a value. If we want our function to return some value to a
function call, we use the return statement.
The return statement also denotes that the function has ended. Any code after return is not
executed.
Python Recursion:
In Python, we know that a function can call other functions. It is even possible for the function to call
itself. These types of construct are termed as recursive functions.
Loops:
While Loops:
A while loop statement in Python programming language repeatedly executes a target statement as
long as a given condition is true.
while (expression):
statement
increment or decrement
When the condition becomes false, program control passes to the line immediately following the
loop.
Infinite loop:
You must be cautious when using while loops because of the possibility that this condition never
resolves to a FALSE value.
For loop:
For statement in Python has the ability to iterate over the items of any sequence, such as a list or a
string.
statements(s)
Range:
list(range(5))--------- [0, 1, 2, 3, 4]
range(5) -------------range(0, 5)
If the else statement is used with a for loop, the else block is executed only if for loops terminates
normally (and not by encountering break statement).
If the else statement is used with a while loop, the else statement is executed when the condition
becomes false.
Nested loops:
Python programming language allows the use of one loop inside another loop.
Ex*
statements(s)
statements(s)
Return statement:
The function return statement is used to exit from a function and go back to the function caller and
return the specified value or data item to the caller.
The return statement can consist of a variable, an expression, or a constant which is returned to the
end of the function execution. If none of the above is present with the return statement a None
object is returned.
If Statement:
if statement is the most simple decision-making statement. It is used to decide whether a certain
statement or block of statements will be executed or not
If – else:
The if statement alone tells us that if a condition is true it will execute a block of statements and if
the condition is false it won’t. But what if we want to do something else if the condition is false. Here
comes the else statement. We can use the else statement with if statement to execute a block of
code when the condition is false.
Nested If:
A nested if is an if statement that is the target of another if statement. Nested if statements mean an
if statement inside another if statement. Yes, Python allows us to nest if statements within if
statements.
if-elif-else ladder :
Here, a user can decide among multiple options. The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed
Code:
if (list1[i] == x):
return i
return -1
l = [2, 4, 0, 1, 9]
x=1
n = len(l)
result = linearSearch(l, n, x)
if(result == -1):
else:
if list1[mid] == x:
return mid
low = mid + 1
else:
high = mid - 1
return -1
l = [3, 4, 5, 6, 7, 8, 9]
Low=0
High=len(l)-1
x=4
if result != -1:
else:
print("Not found")