Python Unit 1 Notes - Introduction to Python & Clean Coding PDF

Summary

These notes provide an introduction to Python programming, including its features, history, and various applications like web development, data analysis, and machine learning. The document also discusses clean code principles and their importance in software development.

Full Transcript

PYTHON Notes by- Miss Farheen Siddiqui Unit 1 “Introduction to Python & Clean Coding” What is PYTHON? Python is a general-purpose, high-level, object-oriented programming language that is used for a variety of tasks, including: Web development, Data analysis, Software developm...

PYTHON Notes by- Miss Farheen Siddiqui Unit 1 “Introduction to Python & Clean Coding” What is PYTHON? Python is a general-purpose, high-level, object-oriented programming language that is used for a variety of tasks, including: Web development, Data analysis, Software development, Machine learning and artificial intelligence, Scientific and numeric computing, Business applications, Blockchain, Everyday tasks Python is popular with developers and coders because it is easy to learn, efficient, and can run on many different platforms. Python software is free to download and integrates well with all types of systems. History of Python The implementation of Python was started in December 1989 by Guido Van Rossum at CWI in Netherland. It was initially designed by Guido van Rossum in February 1991 and developed code (labelled version 0.9.0) to alt.sources by Python Software Foundation. In 1994, Python 1.0 was released with new features like lambda, map, filter, and reduce. Python 2.0 added new features such as list comprehensions, garbage collection systems. On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to rectify the fundamental flaw of the language. It was mainly developed to emphasize code readability, and its syntax allows programmers to express concepts in fewer lines of code. Features of Python Python is a high-level programming language with many features, including:  Open source and free: Python is free to use, download, and modify.  Easy to learn: Python has a simple syntax and is easy to code.  Portable: Python code can be used on different machines without changes.  Large standard library: Python comes with a large standard library that includes modules for web development, database interaction, and more.  Interpreted: Python is interpreted, so the source code is executed line by line.  Dynamically typed: Python doesn't require declaring the type of a variable.  Multi-paradigm: Python supports multiple programming paradigms, including object-oriented programming, procedural programming, and functional programming.  Supports GUI: Python supports graphical user interface programming. Where we can use PYTHON?  Web development: Python is used for both server-side and backend web development.  Software development: Python is used to create software and apps.  Data analysis: Python is used to analyze data.  Machine learning: Python is used in machine learning.  Automating tasks: Python can be used to automate repetitive tasks.  Scientific and numeric computing: Python has tools and libraries for scientific and numeric computing, such as SciPy for mathematics, science, and engineering.  Business applications: Python can be used to build business applications, such as ERP and ecommerce systems.  Blockchain: Python is a strong language for blockchain development due to its flexibility, functionality, and security.  Everyday tasks: Python can be used for everyday tasks, such as organizing finances. Introduction to Clean Code: Clean Code is a term from software development and addresses the clear, comprehensible, traceable, logical and disciplined implementation of code. The goal is to produce software efficiently and effectively, and to design the code so that it is easily readable, changeable, and expandable. What is Bad Code? Bad code is code that is poorly written, difficult to understand, and challenging to maintain. It can be more than just syntax errors or minor bugs Some signs of bad code: Complex, Poorly structured, Lacking documentation, Duplicative, Excessive dependencies, unnecessarily complex, Not scalable, Inconsistent in naming, overly verbose, Untested, Hard to read, has non-intuitive variable names. What is Clean Code? Clean code is written with the intention of helping others understand the purpose of the code so that they can make changes to it in the future.  Clear: The code is self-explanatory and communicates its functionality.  Logical: The code has well-formed instructions that work together.  Formatted: The code has a consistent presentation with regular spacing, indentation, and character placement.  Conventional: The code adheres to language conventions and performs tasks with expected instructions.  Identifiable: The names follow a regular structure based on language conventions.  Responsible: The code is lawful, trustworthy, and respectful. What is indentation? In Python, indentation is the use of spaces or tabs at the beginning of a line of code to group and organize code. Indentation is a requirement in Python, not just a convention. It's a key part of Python's syntax and is used to:  Define blocks of code: Indentation is used to define blocks of code, such as loops or functions.  Determine code flow: Indentation determines the flow of code.  Improve readability: Indentation makes code more readable and easier to understand.  Ensure code cleanliness: Consistent indentation keeps code clean and organized.  Facilitate collaboration: Consistent indentation helps developers collaborate seamlessly. In other programming languages, like C, C++, and Java, curly braces are used to define a block of code. Text Editors: A text editor is a tool that is used to write and edit code. They are usually lightweight and can be great for learning. However, once your program gets larger, you need to test and debug your code, that's where IDEs come in. Some text editors for Python: IDLE, PyCharm, Sublime Text, Visual Studio Code, Jupyter Python Variables:  Variables are containers for storing data values. Variables are used as placeholders for information that can be recalled later in the coding process.  Python has no command for declaring a variable.  A variable is created the moment you first assign a value to it. Input: x=5 y = "John" print(x) print(y) Output: 5 John  Variables do not need to be declared with any particular type, and can even change type after they have been set. Input: x=4 # x is of type int x = "Sally" # x is now of type str print(x) Output: Sally Data-Types: Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of these classes. The following are the standard or built-in data types in Python:  Numeric  Sequence Type  Boolean Set  Dictionary  Binary Types (memoryview , bytearray , bytes) To define the values of various data types of Python and check their data types we use the type() function.  x = "Hello World" #string  x = 50 #integer  x = 60.5 #float  x = 3j #complex  x = ["geeks", "for", "geeks"] #list  x = ("geeks", "for", "geeks") #tuple  x = range(10) #range  x = {"name": "Suraj", "age": 24} #dictionary  x = {"geeks", "for", "geeks"} #set  x = frozenset({"geeks", "for", "geeks"}) #frozenset  x = True #boolean  x = b"Geeks" #bytes  x = bytearray(4) #bytearray  x = memoryview(bytes(6)) #memoryview  x = None #special value Type Casting: There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number) float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer) str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals  Integers: Input: x = int(1) # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3 print(x) print(y) print(z) Output: 1 2 3  Floats: Input: x = float(1) # x will be 1.0 y = float(2.8) # y will be 2.8 z = float("3") # z will be 3.0 w = float("4.2") # w will be 4.2 print(x) print(y) print(z) print(w) Output: 1.0 2.8 3.0 4.2  Strings: Input: x = str("s1") # x will be 's1' y = str(2) # y will be '2' z = str(3.0) # z will be '3.0' print(x) print(y) print(z) Output: s1 2 3.0 Operators: Operators are used to perform operations on variables and values. For example- We use the + operator to add together two values- Input: print(10 + 5) Output: 15 Python divides the operators in the following groups:  Arithmetic operators  Assignment operators  Comparison operators  Logical operators  Identity operators  Membership operators  Bitwise operators Arithmetic operators: Arithmetic operators are used with numeric values to perform common mathematical operations: 1. Addition + x+y Input: x=5 y=3 print(x + y) Output: 8 2. Subtraction – x–y Input: x=5 y=3 print(x - y) Output: 2 3. Multiplication * x*y Input: x=5 y=3 print(x * y) Output: 15 4. Division / x/y Input: x = 12 y=3 print(x / y) Output: 4 5. Modulus % x%y Input: x=5 y=2 print(x % y) Output: 1 6. Exponentiation ** x ** y Input: x=2 y=5 print(x ** y) #same as 2*2*2*2*2 Output: 32 7. Floor division // x // y Input: x = 15 y=2 print(x // y) #the floor division // rounds the result down to the nearest whole number Output: 7 Assignment operators: Assignment operators are used to assign values to variables 1. Assign = Input: x=5 print(x) Output: 5 2. Add and assign + = Input: x=5 x += 3 print(x) Output: 8 3. Subtract and assign - = Input: x=5 x -= 3 print(x) Output: 2 4. Multiply and assign * = Input: x=5 x *= 3 print(x) Output: 15 5. Divide and assign / = Input: x=5 x /= 3 print(x) Output: 1.6666666666666667 6. Modulus and assign % = Input: x=5 x%=3 print(x) Output: 2 7. Floor division and assign // = Input: x=5 x//=3 print(x) Output: 1 8. Exponentiation and assign ** = Input: x=5 x **= 3 print(x) Output: 125 9. Bitwise AND and assign & = Input: x=5 x &= 3 # combines the values of two variables using a bitwise AND operation and assigns the result to the variable on the left. # x = 5: The binary representation of 5 is 101 # x &= 3: The binary representation of 3 is 011 # print(x): The output is 1, which is the binary representation of 001 print(x) Output: 1 10.Bitwise OR and assign | = Input: x=5 x |= 3 # performs a bitwise OR operation on the left and right operands, and assigns the result to the left operand. # = 5: The binary representation of 5 is 101 # x = 3: The binary representation of 4 is 011 # print(x): The output is 7, which is the binary representation of 111 print(x) Output: 7 11.Bitwise XOR and assign ^ = Input: x=5 #101 x ^= 3 #011 # compares two binary values and returns a result based on whether the bits match: # 1: If one input is 1 and the other is 0 # 0: If both inputs are 1 or both are 0 print(x) Output: 6 #110 12.Walrus Operator : = Input: print(x := 3) # We can use this walrus operator to assign a value and do condition check at the same time. So, we can use variable a not just in statement also after that. it will simply assign new value into variable and enables condition check. Output: 3 Comparison operators: Comparison operators are used to compare two values 1. Equal = = Input x=5 y=3 print(x = = y) # returns False because 5 is not equal to 3 Output False 2. Not equal != Input x=5 y=3 print(x != y) # returns True because 5 is not equal to 3 Output True 3. Greater than > Input x=5 y=3 print(x > y) # returns True because 5 is greater than 3 Output True 4. Less than < Input x=5 y=3 print(x < y) # returns False because 5 is not less than 3 Output False 5. Greater than or equal to >= Input x=5 y=3 print(x >= y) # returns True because 5 is greater, or equal, to 3 Output True 6. Less than or equal to 3 or x < 4) # returns True because one of the conditions are true (5 is greater than 3, but 5 is not less than 4) Output True 3. Not Reverse the result, returns False if the result is true Input x=5 print(not(x > 3 and x < 10)) # returns False because not is used to reverse the result Output False Identity operators: 1. Is Returns True if both variables are the same object Input: x = ["apple", "banana"] y = ["apple", "banana"] z=x print(x is z) # returns True because z is the same object as x print(x is y) # returns False because x is not the same object as y, even if they have the same content print(x = = y) # to demonstrate the difference betweeen "is" and "= =": this comparison returns True because x is equal to y Output: True False True 2. Is not Returns True if both variables are not the same object Input: x = ["apple", "banana"] y = ["apple", "banana"] z=x print(x is not z) # returns False because z is the same object as x print(x is not y) # returns True because x is not the same object as y, even if they have the same content print(x != y) # to demonstrate the difference betweeen "is not" and "!=": this comparison returns False because x is equal to y Output: False True False Membership operators: 1. In Returns True if a sequence with the specified value is present in the object Input: x = ["apple", "banana"] print("banana" in x) # returns True because a sequence with the value "banana" is in the list Output: True 2. Not in Returns True if a sequence with the specified value is not present in the object Input: x = ["apple", "banana"] print("pineapple" not in x) # returns True because a sequence with the value "pineapple" is not in the list Output: True Bitwise operators: 1. AND & Sets each bit to 1 if both bits are 1 Input: print(6 & 3) # The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0: 6 = 0000000000000110 3 = 0000000000000011 -------------------- 2 = 0000000000000010 -------------------- Output: 2 2. OR | Sets each bit to 1 if one of two bits is 1 Input: print(6 | 3) # The | operator compares each bit and set it to 1 if one or both is 1, otherwise it is set to 0: 6 = 0000000000000110 3 = 0000000000000011 -------------------- 7 = 0000000000000111 -------------------- Output: 7 3. XOR ^ Sets each bit to 1 if only one of two bits is 1 Input: print(6 ^ 3) #The ^ operator compares each bit and set it to 1 if only one is 1, otherwise (if both are 1 or both are 0) it is set to 0: 6 = 0000000000000110 3 = 0000000000000011 -------------------- 5 = 0000000000000101 -------------------- Output: 5 4. NOT ~ Inverts all the bits Input: print(~3) The ~ operator inverts each bit (0 becomes 1 and 1 becomes 0). Inverted 3 becomes -4: 3 = 0000000000000011 -4 = 1111111111111100 Output: -4 5. Zero fill left shift 1) * 2 2. Evaluate: (10 & 6 | 3 ^ 2 > 1 3. Evaluate: (14 >> 1 ^ 3 & 7 1) * 2

Use Quizgecko on...
Browser
Browser