Basic Notes 1
Basic Notes 1
Operand:
They are the values (or variable holding value) involved in an operation
Operator:
They are special symbols or keywords which has got in-built functionalities
1. Arithmetic op
2. Comparison op
3. Logical op
4. Bitwise op
5. Assignment op
6. Identity op
7. Membership Op
1. Arithmetic op:
+:
-When used with 2 or more numeric values, performs addition operation
eg:
print(3 + 4 + 8 + 9)#addition
print(+3)#positive int
-:
-When used with 2 or more numeric values, performs subtraction operation
eg:
print(3 - 4 - 8 - 9)#subtraction
*:
- When used with atleast 2 numeric operands performs multiplication
eg:
print(10*33)#multiplication
print(2*3*8)#multiplication
print(*3)#TypeError
** [Exponentiation]:
- it helps to return the exponent value of the given base and power
- Syn:
base ** power
- eg:
print(3 ** 2) #9
// [Floor Division]:
as the output
- eg:
print(11 // 2) # 5
print(22 // 7) #3
/ [Float Division]:
as the output
- eg:
print(11 / 2) # 5.5
print(22 / 7) # 3.142857142857143
% [Modulus]:
as the output
- eg:
print(11 % 2) # 1
print(22 % 7) #1
Ex:
print(True == True)#True
print(False != (2**0))#False
print("ap" == "aa")#False
print(97 == "97")#False
[Note:
boolean conditions:
a. boolean values
3. Logical Op:
logical and:
logical or:
logical not:
- It requires only one operand to perform the operation
- It returns the inverted bit of the actual O/P bit
Input Output
True False
False True
- It helps to store a value present in RHS to the named memory location present in LHS
Variables:
- If we try to store multiple values one after the other then variable will only hold
the latest updated value
[Note:
- The combined form of (Arithmetic Op + Assignment Op) or (Bitwise Op + Assignment Op) are called
as compound statements
1. They are used only when the operand used in RHS(in operation) and the operand used in
LHS(for assignment) is same
4. Bitwise Op:
| --> Bitwise OR
1 --> ~1 ==> 0
0 --> ~0 ==> 1
- It is allowed to be used only on iterable obj(The object which stores multiple individual values into
a shared memory.
- It helps to check whether the defined value is available among the iterable obj or not by returning a
boolean value
===================================================================
Control Flow Statements:
- It controls the execution flow of a program
- 2 types:
1. Conditional Statements:
2. Looping Statements:
a. for loop
i.with range()
2 varieties:
incrementing loop
decrementing loop
b. while loop
2 varieties:
incrementing loop
decrementing loop
"offline"
Conditions:
If the withdrawal amount is less than or equal to the balance, allow the transaction and display the
remaining balance.
Scenario 3:Write a program to check the type of triangle based on the sides provided.
Conditions:
Conditions:
Input: Location type (1 for Local, 2 for Regional, 3 for National, 4 for International)
Conditions:
A theme park charges ticket prices based on age and group discounts:
Conditions:
================================================================
default start_val : 0
sequence value
ex:
val = range(1, 5)
#a sequence of values
2. Looping Stmts:
a. for loop:
2 varieties:
============================================================
ii. decrementing for loop:
2. If the programmer does not mention the step, then it takes the default
step. Therefore it is compulsory to include a negative decrementing step.
3. The loop will work until the sequence value is greater than the end_val
============================================================
Note:
-If the start_val == end_val then the loop will not be executed
-In a incrementing loop if start_val > end_val then the loop will not be executed
-In a decrementing loop if start_val < end_val then the loop will not be executed
[Note:
- In a incrementing loop if the mentioned end values also has to be executed then "actual end + 1"
- In a decrementing loop if the mentioned end values also has to be executed then "actual end - 1"]
#logic
Here, the var_name will store the elements of the mentioned object.
WAP to print "Hello World" until and unless the user do not enter 5.
1 HW
100 HW
55 HW
while i >= 10: # 5 >= 10 --> Terminates the loop, bcz it never starts
print(i)
i -= 1
i=5
print(i)
i -= 1
i = 5 ; 5 <= 10 --> T
i = 4 ; 4 <= 10 --> T
1.
n=4
print(i)#5 4 3 2 1
2.
n=3
3.
n = 10
for i in range(1, n + 1, -1):
print(i)
therefore PVM is expecting start > end here, 1 > 11 --> False
Tasks:
I/P: n = 7
O/P: 1 3 5 7
I/P: n = 10
O/P: 10 5
I/P: n = 8
O/P: 5
#WAP to display the natural numbers till the defined "n" value
n=8
possible ans,
start = 1
12345678
start = 3
345678
start = 2
2345678
start = 8
Functions:
eg: print() --> to print if any value passed, and to move the control
to next line
input() --> get the input from user during execution time
Syn:
def function_name(parameters):
#logic
Parts of a function:
2. parameters[optional]: They are the I/P variables required by a function to perform some specific
task
- It majorily helps return back the execution flow from called function back to function call
- It helps to return value / values i.e., O/P from a called function back to the function call, which has
to be received by a variable at function call.
it is not called