Python
Python
Note:If I want to use output of previous operation so we use underscore (_) _+y 8
533
we get 533 in octal.
Use of method in python to direclty convert number system:
Decimal to binary: bin()
e.g bin(51)
'0b110011'
# 0b represent binary formate
binary to decimal: int()
e.g
int('0b110011',2)
51
Decimal to octal: oct()
e.g
oct(51)
'0o63' # 0o represent octal formate
octal to decimal: int()
e.g
int('0o63',8)
51
Decimal to hexadecimal: hex()
e.g
hex(51)
'0x33' # 0x represent hexadecimal formate
hexadecimal to decimal: int()
e.g
int('0x33',16)
51
#1 - In bitwise operations, the integers are first converted into binary and then
operations are performed by bitwise operators on each bit or corresponding
pair of bits.
- The result is returned in decimal format by the bitwise operators. - There are
six types of binary operators:-
1. Complement (~)
2. And (&)
3. Or (|)
4. XOR (^)
5. Left Shift 6. Right Shift
#2 - Complement operator is also known as tilde(~) operator.
- Complement simply do the reverse of binary form. If a bit is 0 then it makes it
1 and vice-versa.
- Reverse of each bit of a number returns the 1's complement.
- We can store only positive numbers in our system and that's why we find the
2's complement of each negative number. 2's complement = 1's complement
+1
#3 -AND operator will return true if both the conditions are true while the OR
operator will return true if at least one condition is true.
#4 - XOR operator will return 1 or true when there is an odd number of 1 bit
present in operations and if there is an even number of 1, then it will return 0.
#5 - Leftshift operator shift bits on the left-hand side. The right-shift operator
shifts bits on the right-hand side. - Leftshift add the bits while the right-side
removes the bits.
math module in Python provides a wide range of mathematical functions.
-- math module is a built-in module in Python.
-- math module is used to perform mathematical operations.
-- math module is used to perform mathematical operations like square root,
power, floor, ceil, etc.
-- for using math module we have to import it first # import math
-- then we can use math module function like math.sqrt(25) or
-- we can use alias name like m=math then we can use m. m.sqrt(25) if we
import math as m or
-- we can import only required function like from math import sqrt,pow
sqrt(25)
pow(2,3)
Here are some of the most commonly used ones methods of math module:
math.sqrt(x): Returns the square root of x.
math.pow(x, y): Returns x raised to the power y.
math.ceil(x): Returns the smallest integer greater than or equal to x.
math.floor(x): Returns the largest integer less than or equal to x.
math.exp(x): Returns the exponential value of x.
math.log(x[, base]): Returns the natural logarithm (base e) of x or the logarithm
of x with the specified base.
math.sin(x), math.cos(x), math.tan(x): Returns the trigonometric sine, cosine,
or tangent of x, respectively.
Here are some of the most commonly used ones attributes(constant) of math
module:
math.pi: Returns the value of pi.
math.e: Returns the value of e.
math.inf: Returns a floating point positive infinity.
math.nan: Returns a floating point “not a number” (NaN) value. using help
function we can get all the information about math module help(math)
#1 How to get user input
-- Getting user input in Python is straightforward. You can use the input()
function to get input from the user. The input function takes a single
argument, which is the prompt message displayed to the user.
e.g
name = input("Please enter your name: ")
x=input("Enter first number: ");
y=input("Enter second number: ");
z=x+y; print(z);
#2 input function
-- In Python, the input() function is used to accept user input from the
command line or console. name=input("Enter your name:"); print(name);
-- In this example, the input() function prompts the user to enter their name.
Whatever the user types in response is stored in the name variable.
-- Note that the input() function always returns a string, so if we want to use
the user's input as a number, we'll need to convert it using the appropriate
type-casting function (e.g., int() for integers or float() for floating-point
numbers).
#3 Types of input data
-- The input() function always returns a string, regardless of what the user
enters. we may need to convert the input to a different data type if you want
to perform calculations or other operations on it.
e.g
x=input("Enter first number: ");
a=int(x);
the input entered by the user is converted to an integer using the int() function
in this example.
#4 when to use index value
-- If you want to get a single character from the user, we can use the input()
function and index the result.
e.g
ch=input('enter a character: ');
print(ch[0])
ch=input('enter a character: ')[0];
print(ch);
#5 eval function eval function
-- The eval() function in Python is used to evaluate an expression entered by
the user as a string. The eval() function returns the result of the expression as a
value.
e.g
x=eval(input("Enter an expression: "));
typeOf = type(x);
print(typeOf);
#6 Passing values from command line
-- sys module provides access to any command-line arguments via the sys.argv
list. we can pass arguments to a Python script from the command line using
the sys.argv list. The first argument in the list is always the name of the script
itself.
suppose we have a file named Mycode.py in file we have written code import
sys # without this line you will get error
x=sys.argv[1];
y=sys.argv[2];
z=x+y;
print(z);
in command line we have to run this file
#python3 Mycode.py 9 5
0 12
Note: Mycode is count as 0th argument
9 is count as 1st argument
5 is count as 2nd argument
#1 –
CPU has three parts:
CU (Control Unit),
ALU ( Arithmetic Logic Unit) and
MU ( Memory unit).
- MU is used to store variables and data.
- ALU has two parts:
1. AU - Arithmetic Unit ( it performs mathematical calculations)
2. LU - Logical Unit ( it makes a computer think something)
#2 If statement:
- - In programming, we have to apply conditions as per the logic of the
code. In python, conditions can be applied through the if keyword.
- Use of the if keyword specifies the flow of execution of the code.
- Based on the condition of the problem statement, if keyword helps to
decide which set of statements should be executed.
Syntax:- if (condition): statement;
- The statements of the if block will be executed only when the
condition of the if statement is true. If the condition is false then it will
skip the execution of statements present inside the if block.
- If consists of a block where you can write multiple statements. In
python, it is also known as Suite.
#2 Indentation:- - In Python, we have to follow certain indentations that
specify the conditions that are present inside a certain block.
- Indentation simply means a certain number of spaces at the beginning
of a code line. - Indentation increases the readability of the code.
#3 Else block:-
- We can also use multiple if blocks in a code.
- Multiple uses of the if block decrease the efficiency of a code as the
condition will be checked again and again in each if block.
- To make the code efficient, we use the else block.
- If the condition of the if block is true then the else block will be
skipped. And if the condition of the if block is false then the else block
will be checked and executed.
#4 Nested if and else statements:-
- Nested if and else statements are also allowed in Python.
- if statement can also be checked inside other if statement. This
conditional statement is called a nested if statement.
- In nested, the inner if condition will be checked only if the outer if
condition is true and that helps to see multiple conditions to be satisfied.
- Round brackets for putting a condition in the if statement is optional.
#5 if, elif and else statements:-
- elif stands for if-else.
- The if-elif statement is a shortcut of if..else chain.
- If the if condition s false, then the condition inside the elif will be
checked and executed.
- While using if-elif statement at the end else block is added that will be
executed when none of the above if-elif statements is true.
#1
- We can add any value in each element of an array.
- We can also add values of two arrays.
- The addition of two arrays is known as Vectorized Operation.
- We can also find the values of each element of an array of
mathematical operations such as sin, cos, tan, log, sqrt etc.
- Sum of every element of an array can also be calculated by using the
sum() function.
- Minimum value can also be get from an array through the min()
function.
- Maximum value can be get from an array by using the max() function.
- Unique elements from an array and the sorted array can also be get.
- Concatenation of two arrays can also be performed by using the
concatenate function. This function combines two arrays.
#2
- Two variables can be pointed towards a single array. It means both
variables are pointing to the same memory address where the array is
stored.
- In Python, Aliasing happens whenever one variable's value is assigned
to another variable.
- view() is a function that helps to create a new array at a new location.
It means arrays should be present at the different memory addresses.
- By using the view() functions, two variables will point towards two
different arrays.
#3
- In python, two copying techniques are available i.e.,
1. Shallow Copy
2. Deep Copy
- In shallow copy, it will copy elements but it means both arrays are
dependent on each other.
- In shallow copy, if we update an element in one array then the changes
would also be reflected in another array.
- In a deep copy, two different arrays are not linked with each other in
any way.
- In a deep copy, we use the function known as a copy() instead of using
the view() function.
- copy() function provides a deep copy of an array.
-- formal argument
-- actual argument
Actual arguments have 4 types:
1)position
2)keyword
3)default
4)variable
length argument Position argument:
-- During a function call, values passed through arguments should be in
the order of parameters in the function definition.
This is called positional arguments.
e.g def person(name,age):
print(name)
print(age)
add(5,6)
keyword argument:
-- During a function call, values passed through arguments don’t need to
be in the order of parameters in the function definition. Keyword
arguments can achieve this.
-- All the keyword arguments should match the parameters in the
function definition.
e.g
person(age=28,name='navin')
default argument:
-- Default arguments are values that are provided while defining
functions.
-- The assignment operator = is used to assign a default value to the
argument.
-- Default arguments become optional during the function calls.
-- If we provide a value to the default arguments during function calls, it
overrides the default value.
-- The function can have any number of default arguments.
-- Default arguments should follow non-default arguments.
e.g
def person(name,age=28):
print(name)
print(age)
person('navin')
variable length argument:
-- if you want to pass multiple value in a function call we can use variable
length argument
def sum(a,*b):
print(a)
print(b)
c=a
for i in b:
c=c+i
print(c)
sum(5,6,34,78)