IO Operations: ============== IO ==> Input-Output Operations Input ==> Reading of data (or) Taking of data which can be entered by the user from Keyboard. Output ==> Writing of data (or) Displaying of data which can be by the program on the screen or console. -> input() ==> can be used for input operations in python -> print() ==> can be used for output operations in python. input(): ======== Syntax: variable-name/Identifier = input("Some Text") a = input("Enter some value:") print(a) -> input() can always read any type of value in string type only by default. -> if we want to process a input value other than the string using input(), we need to perform typecasting to the input() with the type of the value which we want to store in the application. Syntax: variable-name = datatype(input("enter some text:")) a = int(input("Enter an integer:")) b = float(input("Enter a floating-point value:")) c = complex(input("Enter a complex value:")) d = bool(input("Enter a boolean value:")) e = input("Enter some string:") print(type(a)) print(type(b)) print(type(c)) print(type(d)) print(type(e)) -> using input(), if we want to read a binary value: Syntax: identifier = int(input("Enter binary value:"),2) -> using input(), if we want to read an octal value: Syntax: identifier = int(input("Enter an octal value:"),8) -> using input(), if we want to read an hexadecimal value: Syntax: identifier = int(input("Enter hexadecimal value:"),16) a = int(input("Enter a decimal value:"),2) b = int(input("Enter an octal value:"),8) c = int(input("Enter a Hexadecimal value:"),16) print(type(a)) print(type(b)) print(type(c)) -> variable can define in two ways: 1) Variable in compile time ==> a value of the variable can be fixed in the entire program. Ex: a = 123 print(a) # 123 2) Variable in run time ==> for every execution time, if we want to pass different value to the variable then, we can define that variable in run time. ================================================== print(): ======= -> print() can always print any value in decimal format only. a = int(input("Enter a decimal value:"),2) b = int(input("Enter an octal value:"),8) c = int(input("Enter a Hexadecimal value:"),16) print(a) print(b) print(c) -> if we want to print the values other than decimals: a = int(input("Enter a decimal value:"),2) b = int(input("Enter an octal value:"),8) c = int(input("Enter a Hexadecimal value:"),16) print(bin(a)) print(oct(b)) print(hex(c)) -> If we want to print multiple values within the same line: a = int(input("Enter a decimal value:"),2) b = int(input("Enter an octal value:"),8) c = int(input("Enter a Hexadecimal value:"),16) print(a,b,c) -> If we want to print multiple values in multiple lines but using single print(): a = int(input("Enter decimal:")) b = float(input("Enter float:")) c = bool(input("Enter boolean:")) print(a,'\n',b,'\n',c) -> If we want to print a text along with value: a = int(input("Enter decimal:")) b = float(input("Enter float:")) c = bool(input("Enter boolean:")) print("The Integer value = ",a) print("The Float value = ",b) print("The Boolean value = ",c) -> A text along with values with single print(): a = int(input("Enter decimal:")) b = float(input("Enter float:")) c = bool(input("Enter boolean:")) print("The Integer Value = ",a,"\nThe Float Value = ",b,"\nThe Boolean Value = ",c) Printing of data along with text in a formatted pattern: ======================================================== Approach-1: ============ using % ======== a = int(input("Enter decimal:")) b = float(input("Enter float:")) c = bool(input("Enter boolean:")) print("The Value of a = %d The Value of b = %d and The Value of c = %d"%(a,b,c)) print("The Value of a = %s The Value of b = %s and The Value of c = %s"%(a,b,c)) print("The Value of a = %i The Value of b = %f and The Value of c = %s"%(a,b,c)) print("The Value of a = %s The Value of b = %s and The Value of c = %s"%(c,a,b)) using format() ============== a = int(input("Enter decimal:")) b = float(input("Enter float:")) c = bool(input("Enter boolean:")) print("Value of a = {}\nValue of b = {}\nValue of c = {}".format(a,b,c)) print("Value of a = {0}\nValue of b = {1}\nValue of c = {2}".format(a,c,b)) print("Value of a = {p}\nValue of b = {q}\nValue of c = {r}".format(r = c,p = a,q = b)) ================================================================ Operators: ========== 12 * 4 ==> Expression 12 and 4 ==> Operands/Values * ==> Symbol ==> operator -> An operator is a symbol which denote an operation -> According to the operands: the operators are classified into three types: 1) Unary Operators 2) Binary Operators 3) Ternary Operators -> When an operator can define with single operand, then it is called "Unary Operator" Ex: -2 ==> here: '-' for sign representation -> When an operator can define with two operands, then it is called "Binary Operator". Ex: 12 + 13 ==> '+' is the binary operator -> When an Operator can define with more than two operands, then it is called "Ternary Operator". Ex: Conditional operator -> Based on the general usage, the operators are classified into several types: 1) Arithmetic Operators 2) Assignment Operators 3) Relational Operators 4) Logical Operators 5) Bitwise Operators 6) Conditional Operator 7) Special Operators 1) Arithmetic Operators ======================= a = 10 b = 3 print("sum = ",a+b) print("subtraction = ",a-b) print("multiplication = ",a*b) """ three type division operators: 1) Normal Division operator ==> / ==> Quotient in float 2) Floor Division Operator ==> // ==> Quotient in integer 3) Modulo Division Operator ==> % ==> Remainder """ print("Quotient in Float = ",a/b) print("Quotient in Integer = ",a//b) print("Remainder = ",a%b) print("Exponent Operation = ",a**b) ============================================== 2) Assignment Operator: ======================= x = 12 # 12 is the value is assigned to the variable 'x' -> the assignment operator can join with other operators like arithmetic operators to form "compound operators". like: +=, -=, *=, /=,//=,%= and **= x = 1 print(x) # 1 # x = x + 10 x += 10 print(x) # 11 x = 1 print(x) # 1 # x = x + 10 x += 10 #11 print(x) # x = x - 2 x -= 2 print(x) # 9 # x = x ** 2 x **= 2 print(x) # 81 =========================================== Relational Operators: ===================== -> also called as "Comparison Operators". 7 9 7 is lesser than 9 ==> True 7 is lesser or equal than 9 ==> True 7 is equal to 9 ==> False 7 is not equal to 9 ==> True 7 is greater than 9 ==> False -> Like: <, >, <=, >=, == and != -> after the comparison, these operators can always return a Boolean value (True/False). x = 9 y = 7 print(x == y) """ == ==> Equal operator ==> for compariso = ==> Assignment operator ==> Assignment If we can use '==' in the place of '=' or '=' in the place of '==' ==> Syntax Error/Run Time Error """ # # z == 19 # Name Error # print(x = y) # Type Error print(x != y) print(x < y) print(x > y) ======================================================= Note: ===== In Java: Increment and Decrement operators Increment operators: ++ Pre Increment Post Increment Decrement Operators: -- Pre Increment Post Increment --> all these are Unary operators. --> In python there are no increment and decrement operators available. =============================================================== Logical Operators: ================== -> three types of logical operators: 1) logical and ==> and keyword =============================== # logical and operator # binary operator # if any input is "False", then: the output ==> False print(True and True) # True print(True and False) # False print(False and True) # False print(False and False) # False # How the logical and can work? # if the first input (operand1 or left operand) is "True", then: # logical and operator can check the second input (operand2 or right operand) # based on the second operand the output should be. (output is same as second operand). # if the first operand is "False", # logical and operator never read the second operand. # and the output is same as "first operand". print(-10 and 10) # 10 print(10 and -10) # -10 print(0 and 10) print(-10.2 and 0.1) print(1-2j and 1+2j) print('a' and 'b') print('a' and '') print("hi") print('' and 'r') print("Bye") print(None and 'a') print(123 and None) 2) logical or ==> or keyword ============================= # Logical Or # binary operator # when any input is "True", output ==> True # whe both inputs are "False", then: # output ==> False print(True or True) # True print(True or False) # True print(False or True) # True print(False or False) # False # How the logical or can work? """ if operand1 ==> True then: result ==> Operand1 without decoding/reading the operand2 if operand1 ==> False: output ==> operand2 """ print(0 or 10) # 10 print(10 or 0) # 10 print(-10 or 10) # -10 print(10 or -10) # 10 3) logical not => not keyword ============================== # Logical Not # unary operator print(not True) print(not False) print(not 10) print(not 0) print(not 'a') print(not '') =============================================================== Special Operators: ================== 1) Identity Operators ===================== -> two types: 1) is 2) is not a = 10 b = 10 c = 20 # id() ==> can return an address of an element print("address of a = ",id(a)) print("address of b = ",id(b)) print("address of c = ",id(c)) # identity operators are always compare the addresses of elements. """ is operator: ========== True ==> if two objects address locations are same False ==> if two objects address locations are different is not ====== True ==> if two objects address locations are different False ==> if two objects address locations are same """ print(a is b) print(a is c) print(a is not b) print(a is not c) 2) Membership Operators ======================= Ex: s = "Python" 'y' is belonging to s or not? ==> to check the membership of the given element in the collection or group of values. ==> Two types: 1) in 2) not in s = "Python is Awesome" l = [1,2,3,4,5] print("Awesome" in s) print("awesome" in s) print(7 in l) print("Awesome" not in s) print("awesome" not in s) 3) range operator ================= -> kind of special operator can use to generate the numbers from the given range. Syntax: 1) range(stop) ==> generate numbers from 0 to stop-1 ex: range(10) ==> 0 to 9 2) range(start, stop) ==> generate numbers from 'start' to 'stop-1' Ex: range(1,100) ==> 1 to 99 3) range(start, stop, step) ==> generate numbers from start to stop-1 with the difference of 'step' Ex: range(1,15,3) ==> 1,4,7,10,13 x = range(10) y = range(1,100) z = range(1,15,3) print(x) print(y) print(z) x = list(range(10)) y = tuple(range(1,10)) z = set(range(1,15,3)) print(x) print(y) print(z) ================================================ Conditional Operator: ==================== In Java/C/C++: conditional operator syntax: Variable = (Test Expression) ? expression-1 : expression-2; In Python: Conditional Operator Syntax: variable = Expression-1 if Test_Expression else Expression-2 -> also called as "Ternary Operator" # WAP TO CHECK WHETHER THE GIVEN NUMBER IS POSITIVE OR NEGATIVE. num = int(input("Enter a value:")) result = "Number is Positive" if num > 0 else "Number is Negative" print(result) # WAP to checck whether the given number is Even or odd result = "Number is Even." if num % 2 == 0 else "Number is Odd." print(result) ======================================================= Bitwise Operators: ================== -> these operators can use to define the operations on the data bit by bit. -> The bitwise operators are: 1) bitwise and ==> & 2) bitwise or ==> | 3) bitwise xor ==> ^ 4) bitwise complement ==> ~ 5) left shift operator ==> << 6) right shift operator ==> >> Note: ===== Unsigned Right Shift operator ==> >>> ==> Supported in Java/JavaScript Unsigned Left Shift Operator ==> <<< ==> Not supported in Java/But supported in JavaScript. -> Python does not have unsigned left shift and unsigned right shift also. -> The bitwise operations can always allow to define with any type of integer value and Boolean only. -> But, these operators cannot allow to define with other datatypes like: float, complex, string. a b a & b a | b a ^ b ======================================= 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 bitwise and =========== Ex: 9 & 10 9 ==> 0b1001 10 ==> 0b1010 ================= 0b1000 ==> 8 bitwise or ========== Ex: 9 | 10 1001 1010 ==== 1011 ==> 11 bitwise XoR: =========== Ex: 9 ^ 10 1001 1010 ==== 0011 ==> 3 Bitwise Complement: ==================== -> Unary Operator -> Can invert the input. +eve ==> change to -eve -eve ==> change to +eve Left Shift Operator: ==================== -> Binary operator Syntax: data << n here: n ==> number of times -> because of left shift operation, the original value can be doubled. -> formula: data X 2^n Right Shift Operator: ====================== -> Binary Operator Syntax: Data >> n -> Because of the right shift, the value can be halved. Formula: data // 2^n print(9 & 10) print(9 | 10) print(9 ^ 10) print(~0) print(~10) print(~-7) print(7 << 1) print(7 << 2) print(7 >> 1) print(7 >> 2) Assignment: =========== 1) WAP IN PYTHON USING CONDITIONAL OPERATOR TO CHECK WHICH NUMBER IS BIGGEST AMONG TWO INTEGERS. 2) WAP IN PYTHON USING CONDITIONAL OPERATOR TO CHECK WHETHER THE TRIANGLE IS POSSIBLE WITH GIVEN THREE SIDES OR NOT. Hint: side1 = a = -3 side2 = b = 4 side3 = c = 5 test condition = a + b > c and a + c > b and b + c > a