PYTHON FULL NOTES F o r a l l s t u d e n t s Definition Python is a high level object oriented programming language that s used for many tasks including web development data analysis and machine learning It s known for being easy to learn and use and is often used as an introductory programming language What Python is used for Web development Python is used to create web applications on a server Data analysis Python is used to analyze data Machine learning Python is used to build machine learning models Software development Python is used to build software System scripting Python is used to automate tasks Why python is popular Easy to learn Python s syntax is simple and readable which reduces the cost of program maintenance Portable Python can run on many different platforms including Windows Linux and macOS Open source Python is an open source community language so it s constantly being updated and improved by independent programmers Who created Python Guido van Rossum created Python in 1991 The name comes from the British comedy series Monty Python s Flying Circus Variables In Python variables are used to store data values Here s a breakdown of the key points Creating Variables Unlike some languages Python doesn t require explicit variable declarations You create a variable simply by assigning a value to it Python x 10 name Alice Naming rule Variable names can contain letters digits and underscores They must start with a letter or an underscore Variable names are case sensitive myVar and myvar are different variables Avoid using Python keywords e g if else for as variable names Types of Variables Python is dynamically typed meaning the type of a variable is determined at runtime You don t need to specify the type when creating a variable Common data types include float Floating point numbers e g pi 3 14 str Strings e g name Alice bool Boolean values True or False dict Key value pairs e g person name Alice age 30 list Ordered collections e g numbers 1 2 3 int Integers e g x 10 Variable scope Local variables Defined within a function and accessible only inside that function Global variables Defined outside of any function and accessible from anywhere in the program Nonlocal variables Used in nested functions to access variables in the enclosing scope Example x 10 Global variable def my function y 5 Local variable print x Access global variable print y Access local variable my function Point Python is case sensitive so myVar and MyVar are different variables Use meaningful variable names to improve code readability Classes in python In Python a class is a blueprint for creating objects It defines a set of attributes and methods that the objects of that class will have Defining a Class To define a class you use the class keyword followed by the class name and a colon The body of the class contains the attribute and method definitions Example Class Dog def init self name breed self name name self breed breed def bark self print Woof Explanation Class Dog This line defines a class named Dog Def init self name breed This is the constructor method It gets called when you create a new object instance of the class It initializes the object s attributes name and breed Self name name This line assigns the value of the name parameter to the name attribute of Self breed breed This line assigns the value of the breed parameter to the breed attribute of Def bark self This is a method that defines the behavior of the Dog object In this case it the object the object simply prints Woof Creating object To create an object of a class you call the class name like a function passing any necessary arguments to the constructor Accessing Attributes and Methods Example My dog Dog Buddy Labrador You can access the attributes and methods of an object using dot notation Example Print my dog name Output Buddy print my dog breed Output Labrador my dog bark Output Woof Intiger and string Integer int A whole number positive or negative without any decimal point Example 5 10 100 A sequence of characters enclosed within single double or triple quotes Example Hello World Multiline String String str In Python operators are special symbols that perform operations on variables and values Here s a breakdown of different types of operators Operators Subtraction 1 Arithmetic Operators Addition Multiplication Division Modulo returns the remainder Floor Division returns the integer part of division Exponentiation 2 Comparison Operators Equal to Not equal to Greater than Less than Greater than or equal to Less than or equal to 3 Assignment Operators Assigns a value Adds and assigns Subtracts and assigns Multiplies and assigns Divides and assigns Modulo and assigns Floor divides and assigns Exponentiates and assigns and Returns True if both operands are True or Returns True if at least one operand is True not Inverts the logical state of the operand 4 Logical Operators in Checks if a value is present in a sequence e g list tuple string 6 Identity Operators is Checks if two objects are the same object memory location is not Checks if two objects are not the same object 5 Membership Operators not in Checks if a value is not present in a sequence Python has several built in data type Numeric Types float Represents floating point numbers numbers with decimal points e g 3 14 2 7 complex Represents complex numbers e g 2 3j int Represents integers whole numbers e g 1 2 10 Set Types Mapping Type Sequence Types str Represents strings sequences of characters e g Hello Python List Represents ordered mutable changeable collections of items e g 1 2 three Tuple Represents ordered immutable unchangeable collections of items e g 1 2 three Range Represents a sequence of numbers e g range 1 5 dict Represents key value pairs e g name John age 30 set Represents an unordered collection of unique items e g 1 2 3 Frozenset Represents an immutable set e g frozenset 1 2 3 Boolean Type bool Represents a Boolean value True or False e g True bytes Represents a sequence of bytes e g b Hello Bytearray Represents a mutable sequence of bytes e g bytearray b Hello Memoryview Represents a memory view of a bytes like object Binary Types If else statement In Python if else statements are used for conditional execution of code It allows you to execute different code blocks based on whether a condition is true or false Syntax If condition code to execute if the condition is true else code to execute if the condition is false Example X 10 if x 5 print x is
View Full Document