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()