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 3) range operator