Python Programming Fundamentals ================================ Python Syntax ============= Syntax ==> Set of Rules/Guidelines Keywords Identifiers Variables Datatypes IO Operations Operators Control Statements Keywords ======== ==> Are reserved words ==> Pre-defined words ==> can be defined with specific meaning. Selections: =========== n % 2 == 0 ==> Even n % 2 != 0 ==> Odd if n%2 == 0: even else: odd ==> 35-keywords ==> Keywords of python defined in a module named as "keyword" with the reference of "kwlist". ==> to access all keywords of python, we must import "keyword" module. Syntax: import keyword python-kywords = keyword.kwlist import keyword keywords = keyword.kwlist print(keywords) ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] ================================================== Identifiers: ============ Ecommerce ==> Kart p1 p2 p3 p4 -> identifier is a name, which can be used to identify the entities of the python program like: variables, functions, classes, objects, methods etc. Naming Conventions: =================== 1) Identifiers should define with: Alphabets Digits Underscore sign only. 2) The identifier should always start (first character identifier) with either an alphabet or an underscore sign but not with the digit. Ex: 12abc = 10 ==> Syntax Error abc = 123 _12abc = 321 # 12abc = 4321 3) Identifier cannot define with a keyword. 4) No special characters like: space, period etc. can use in an identifier. 5) Identifiers are case sensitive. # import = 231 # abc 123 = 12344 Import = 123 print(Import) =========================================================== Variables: ========== -> variable is a named memory which can use to store value of any type. -> when we want to use variable in python: we just use assignment only no need to use declaration or initialization why: because the Python is dynamically typed programming language. Syntax for variable definition: identifier = value Is Python Dynamically Typed Language? ======================================= Yes. because, while the data definition in the program, no need to specify the type of the data before the variable name like other programming languages. Python can detect the type based on the assigned value to the variable. type(): ====== it is a built-in function, which can use to know the type of any value. Syntax: type(value) a = 123 b = 1.23 c = True print(type(a)) print(type(b)) print(type(c)) In C: ==== data: Syntax: datatype identifier; // declaration identifier = value; // assignment datatype identifier = value; // Initialization ================================================ Datatypes: ========== -> represents the type of the data can store into variables. -> classified into two types: 1) Primitive datatypes ==> All python basic/fundamental datatypes ==> can store single value in the variable. ex: a = 10 ==> Primitive type 2) Non-Primitive datatypes ==> Collections ==> can store more than one value into variable. ex: a = 10,20,30,40,50 ==> non-primitive types Primitive datatypes =================== -> classified into: 1) Numeral Type ==> deals with numbers a) Integer type b) floating-point type c) complex type 2) Non-Numeral Type a) Boolean type 3) Text Type a) String Type 4) None Type Note: ==== For all python datatypes, we have a pre-defined or built-in class. That means, any definition in python is considered as an Object. Integer type ============ -> built-in class ==> 'int' -> int class describe that integer can define in four ways: 1) decimal type 2) binary type 3) octal type 4) hexadecimal type -> decimal type is also called as "Base-10 number" because, here the value can be defined with only 10 numbers (0 to 9). Ex: 12345678, -123 -> Binary type is also called as "Base-2 number". because, here the value for the variable can be defined with 2 numbers only (0 and 1). -> binary number can prefix '0B' or '0b'. ex: 0b101010 -> Octal type ==> Base-8 number because the value can use to define with numbers (0 to 7). -> Octal numbers must be prefix with '0O' or '0o'. ex: 0o7612 -> Hexadecimal numbers ==> Base-16 numbers we can define a value with: digits (0 to 9) alphabets (a to f/A to F) ==> Alpha numeric value -> hexadecimal number can always prefix with '0X' or '0x'. ex: 0xaf123 a = 123 # decimal b = 0b11001 # binary c = 0O1723 # Octal d = 0xaf123 # Hexadecimal print(type(a),type(b),type(c),type(d)) ============================================================================ floating-point type ==================== float value ==> combination of decimal part and fractional part ex: 102.234 ==> Floating-point value 102 ==> decimal part .234 ==> fractional part -> a built-in datatype because there is a pre-defined class "float". -> while defining the float value: we can consider only the decimal numbers a = 102.234 # b = 0b101.110 # b = 0O127.123 # b = 0Xaf.12 print(type(a)) -> Floating-point value can also allowed to define with scientific form/exponential form. Ex: a = 1.2e5 b = 1.2E-5 c = 150000000000000000000000 c = 15 X 10^22 c = 15/100 X 100 X 10^22 c = 0.15 X 10^2 X 10^22 c = 0.15 X 10^24 c = 0.15E24 d = 0.00000000000000000000000000015 ==> d = 15 X 10^-27 ==> d = 15/100 X 100 X 10^-27 ==> d = 0.15 X 10^2 X 10^-27 ==> d = 0.15 X 10^-25 ==> d = 0.15e-25 a = 0.15e7 b = 1.5e-7 c = 102e9 print(type(a)) print(type(b)) print(type(c)) Note: ==== In an Exponential form, we have two terms: 1) Mantissa ==> the number before the 'e' 2) Exponent ==> the number after the 'e' ==================================================================== Complex Type: ============= -> Built-in type in python because it has the pre-defined class: "complex" -> combination of real and imaginary numbers. Syntax for complex number definition: real-part +/- imaginary-part Note: ===== Imaginary number in python must be suffixed with 'j'. Rules for defining of complex numbers: ====================================== Real-part: ========== 1) Real number of the complex number should be with any integer type of data. 2) We can consider the floating-point value as real value in complex number. Imaginary Part: =============== 1) Imaginary number from the complex number must be with either decimal or float only. a = 102+210j # real number with decimal and imaginary number with decimal b = 0B11001-10101j # real numebr with binary and imaginary with decimal c = 0O123 + 1.2j # real number with octal and imaginary with float d = 0Xaf12 - 123j # real number with hexadecimal e = 102.234 - 123j # real number with float print(type(a)) print(type(b)) print(type(c)) print(type(d)) print(type(e)) ======================================================== Boolean type ============ -> Built-in datatype because, there is a pre-defined class "bool" -> there are two literals/values are possible with Boolean type: 1) True 2) False Note: ===== Python have 35-keywords classified into two parts: 1) Pre-defined literals ==> 3 (True, False and None) 2) Pre-defined words ==> 32 -> Internally: True ==> 1 False ==> 0 a = True b = False print(type(a)) print(type(b)) print(a+a) print(a-a) print(a * b) ====================================================== Text Type: ========== -> also called as "String type" -> String ==> group of characters must be enclosed with single quotes or double quotes or triple quotes. ex: 'a', "a" 'abc', "abc" '''abcd''', """abcd""" Note: ==== In Other programming languages like C, java: we have character but in python the character definition also consider as "string". -> String type is a built-in datatype because it has a pre-defined class "str" a = 'a' b = "b" c = '''r''' d = """rk""" e = "python" print(type(a)) print(type(b)) print(type(c)) print(type(d)) print(type(e)) ============================================= None Type: ========= -> Built-in datatype because it also as a pre-defined class i.e. 'NoneType' -> when you want to just declare a variable or define a variable without assigning of any value, then that variable can allow to assign with 'None'. a = None print(type(a)) print(a) # print(a+a) a = 100 print(a+a) ====================================================================== Type Conversion ================= -> procedure can use to convert the primitive data from one type to another type. -> also called as "type casting". -> Two types of type conversion procedures: 1) Implicit Type conversion/Automatic Type casting 2) Explicit Type Conversion Implicit Type conversion -======================= -> here, the system can automatically perform the conversion from one type to another type. a = 10 b = 3 c = 0 print(type(a)) print(type(b)) print(type(c)) c = a/b print(c,type(c)) Explicit Type Conversion ========================= -> the programmer based on the requirement can perform the conversion of the data from one type to another type. -> In python, the explicit type conversion can be define with some built-in functions: 1) int() 2) float() 3) complex() 4) bool() 5) str() int(): ===== -> when we need to convert any primitive type into an integer type, we can use "int()". Syntax: int(value) Rules: ====== 1) When we want to convert any base value of an integer type into decimal, we can use "int()". binary ==> decimal octal ==> decimal hexadecimal ==> decimal 2) int() can also use to convert a floating-point value into decimal value. 3) int() cannot convert a complex number into integer/decimal. 4) int() can also use to convert a Boolean value into decimal value. True ==> 1 False ==> 0 5) if a string with decimal value integer conversion possible. But if a string with other than decimal integer conversion is not possible. # binary value into a decimal value a = 0b11001 print("Decimal value for the given binary value = ",int(a)) # octal value into a decimal value b = 0O123 print("Decimal value for the given octal value = ",int(b)) # hexadecimal value into decimal c = 0Xaf print("Decima value for the given hexadecimal value = ",int(c)) # floating-point value into decimal d = 123.234 e = 17e-5 print("Decimal of given float is = ",int(d)) print("Decimal of given float is = ",int(e)) # f = 12-23j # print(type(f)) # print(int(f)) # Boolean to integer f = True g = False print(int(f)) print(int(g)) # String to decimal h = '123' print(type(h)) print(type(int(h))) # i = '12.34' # i = 'qa' # print(int(i)) ========================================= float(): ======== -> when we want to convert any value into floating-point value, we can use "float()". Syntax: float(value) Rules: ===== 1) Any integer type (binary/octal/hexadecimal/decimal) possible to convert into an float. 2) We cannot convert a complex number into float. 3) Boolean value possible to convert into float. True ==> 1.0 False ==> 0.0 4) If a string with "decimal" or with "float", possible to convert into float. But a string with other than decimal or float, not possible to convert into float. a = 123 # decimal b = 0b1001 # binary c = 0o123 # octal d = 0xaf1 # hexadecimal print("The Float values after the conversion are:") print(float(a)) print(float(b)) print(float(c)) print(float(d)) # print(float(12.3-23j)) # boolean to float print(float(True)) print(float(False)) e = '123' f = '123.234' print(type(float(e))) print(type(float(f))) print(float(e)) print(float(f)) # print(float('12-23j')) # print(float('True')) ============================================ bool(): ====== -> when we want to convert any value into Boolean value, we can use "bool()". Syntax: bool(any value) -> Any non-zero value (positive or negative) can convert into "True" Ex: bool(10) or bool(-10) ==> True But the zero value, can return "False" by bool(). Ex: bool(0) ==> False print(bool(10)) print(bool(-10)) print(bool(0)) print(bool(1.23)) print(bool(-1.23)) print(bool(0.0)) print(bool(1j)) print(bool(-1j)) print(bool(0j)) # string is empty ==> zero by bool() # string with something ==> non-zero value print(bool('a')) print(bool('')) ========================================== complex(): ========== -> any number want to convert into complex number, we can use "complex()". Syntax: complex(any value) Rules: ======= 1) Any integer value can possible to convert into complex. 2)Floating-point value ==> complex 3) Boolean value ==> complex 4) String with decimal or float or complex ==> complex but string with alphabets ==> not possible into complex print(complex(123)) # decimal ==> complex by adding "0j" as an imaginary to that number. print(complex(0b11001)) # binary ==> decimal ==> complex by adding "0j" as imaginary print(complex(0o12)) print(complex(0xaf)) print(complex(12.23)) # float ==> complex by adding "0j" as imaginary. print(complex(True)) # 1 + 0j print(complex(False)) # 0 + 0j ==> 0j print(complex('100')) print(complex('123.234')) print(complex('12.32-23.34j')) # print(complex('True')) ========================================== str(): ====== -> any value want to convert into a string, we can use "str()". Syntax: str(any value) print(type(str(100))) print(type(str(123.098))) print(type(str(True))) print(type(str('123.234'))) ======================================================= Base Conversions: ================= -> when want to convert any base value into another base value, we can use "base conversions". Ex: Binary ==> octal Binary ==> hexadecimal etc. -. three built-in functions for base conversions: 1) bin() 2) oct() 3) hex() 1) bin(): ========= any base vale can convert into binary using bin(). Syntax: bin(any base value) 2) oct(): ======== Syntax: oct(any value) 3) hex(): ========= Syntax: hex(any value) print(bin(100)) print(bin(0o123)) print(bin(0XAF)) print(oct(0b1010101)) print(oct(123)) print(oct(0x123)) # hex ==> dec ==> oct or hex ==> binary ==> oct print(hex(123)) print(hex(0b11001)) print(hex(0o123))