“Lecture on Python” by Dr. Ravikiran A. Mundewadi Assistant Professor Department of Mathematics M.E.S College of Arts, Commerce and Science, Malleswaram, Bengaluru-03
Guido van Rossum
• Guido Van Rossum is a Dutch programmer best known as the creator of the python programming language. When he began implementing python, Guido Van Rossum was also reading the published scripts from “Monthly Phythan’s Flying Circus, a BBC comedy series from the 1970’s. • Van Rossum thought he needed a name that was short, unique and slightly mysterious, so he decided to call the language Phythan.
What is Python? • Python is a high-level, general-purpose programming language with an elegant syntax that allows programmers to focus more on problem-solving than on syntax errors. One of the primary goals of Python Developers is keeping it fun to use. Python has become a big buzz in the field of modern software development, infrastructure management, and especially in Data Science and Artificial Intelligence. Most recently, Python has risen to the top 3 list of TIOBE index of language popularity. • Python is becoming increasingly ubiquitous, but you must be wondering why Python has become such a hot topic in the developers’ world. In this tutorial, you will understand all reasons behind Python’s popularity.
• It is often used as a “Scripting language ” for web applications. This means that it can automate specific serious of tasks, making it more efficient. Consequently python is often used in software applications, pages within web browser, the shells of operating systems and same games. What is python mainly used for?
Why is python so popular? • More productive. First and foremost reason why python is so much popular is so much popular because it is highly productive as compare to other programming language like C++ and JAVA, etc. Python is also very famous for its simple programming syntax, code readability and English like commands that make coding in python lot easier and efficient.
www.python.org
Download for Windows
Step 1: Download Python 3.9.1 • To start, go to python.org/downloads and then click on the button to download the latest version of Python:
Step 2: Run the .exe file • Next, run the .exe file that you just downloaded:
Step 3: Install Python 3.9.1 • You can now start the installation of Python by clicking on Install Now:
IDLE: Integrated Development and Learning Environment
OUTPUT: Immediate mode • >>> indicates ‘Prompt’
Open the New File
INPUT: Script mode
• This is how the syntax would look like in the “untitled” box:
• Press F5 on your keyboard. You will then get the following message to save your code: • Click on ‘OK’ to save the file • Choose a location where the Python file will be saved on your computer. You’ll also need to type a name for your file. For example, type “Test” for your file name:
Save the File Name • It Save as “Test.py” • “.py” indicates ‘Extension’ • Once you’re done, press Save, and you’ll then see the “Hello World” expression printed on your Python Shell:
Python Shell: • It gives the output as “Hello world”
INSTALLING PACKAGES FOR PYTHON • 1. Go to search and type: cmd command prompt or windows powershell is opened • 2. Type: python -m pip install --upgrade pip • 3. pip install numpy • 4. pip install sympy • 5. pip install matplotlib • 6. pip install scipy
Command Prompt
Windows PowerShell
Pip Installation
What is PIP? • PIP is a package manager for Python packages. • Pip is one of the most famous and widely used package management system to install and manage software packages written in Python and found in Python Package Index (PyPI). Pip is a recursive acronym that can stand for either "Pip Installs Packages" or "Pip Installs Python". Alternatively, pip stands for "preferred installer program".
Fundamentals of Python
Statements • Python statements are nothing but logical instructions that interpreter can read and execute. It can be both single and multiline. There are two categories of statements in Python: • Expression Statements • Assignment Statements
Expression Statement
Assignment Statements
Indentation
Comments
Variables
Constants
Tokens • Tokens are building blocks of a language. They are the smallest individual unit of a program. There are five types of tokens in Python.
Keywords • Key words: Key words are pre defined words with special meaning and these are reserved words must not be used as normal identifier names. These key words consists of False, True, None, and, or, not, as, is, in, if, elif, else, for, while, assert, del, break, class, continue, def, except, global, finally, from, import, lambda, non local, pass, return, try, with, yield.
Identifiers • Identifiers are the names given to variables, objects, classes, functions, lists and so on. • Variables names must not be key words and must not contain blank spaces. • Variable names are made up of (A-Z, a-z, 0-9, underscore_) and must not start with a digit. Valied variables names are ABCD, abcd, AbCd, A345, a_bG78, _product. • Invalid variable names are 6ab, av fd, for, and.
Literals
String Literals: A string literal is a sequence of characters surrounded by quotes. We can use both single, double or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes. Numeric Literals: Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types Integer, Float, and Complex. Boolean Literals: A Boolean literal can have any of the two values: True or False. Collection literals: There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals. Special literals: Python contains one special literal i.e. None. We use it to specify to that field that is not created.
Punctuators or Separators • ‘ indicates single quote • “ indicates double quote • # indicates for comment • indicates backslash • ( ) indicates parentheses • [ ] indicates square brackets • { } indicates braces • : indicates colon • , indicates comma
Input and Output Statement • Input statement : input() is used to input string, int(input()) is used to input integer and float(input()) is used to input floating value. • Output statement: • print statement : • a=2 • b=3 • The print statement is a function its general form is • Print(‘this is’,a,’that is’,b) • Where a and b are pre assigned variable names. If the above statement is executed the output will be as , this is 2 that is 3, anything written between two single quotes or between two double quotes are considered as string.
Lists, Tuple and Functions Lists: Lists are defined as a= [ 1,2,3,4], indexing start with 0 i.e. a[0]=1,a[1]=2,a[2[=3, a[3]=4. The elements of the list may be any data type and are mutable. Tuple : Tuples are the group of any data types separated by commas and are enclosed in parenthesis.The elements of tuple are immutable. Example: a=(1,2,3), b=( ‘a’,2,”C”). Functions : Functions are defined as def name(arguments): return expression Example: def f(x,y): return x+y+2
Operators
Arithmetic Operators Arithmetic Operators Operator Name Description Example + Addition Perform Addition I=40, J=20 >>>I+ J >>>60 - Subtraction Perform Subtraction I=40, J=20 >>>I – J >>>20 * Multiplication Perform Multiplication I=40, J=20 >>>I * J >>> 800 / Division Perform Division I=30, J=20 >>>I /J >>> 2.5 % Modulus Perform remainder after Division I=40, J=20 >>>I /J >>> 0 ** Exponent Performs exponential (power) calculation I=4, J=20 >>>I /J >>> 204 // Floor Division Perform division remove the decimal value and return Quotient value I=30, J=20 >>>I//J >>> 1
Assignment Operators Operator Operator Name Description Example = Assignment It assigns value from right side operand to left side operand I = 40 It assigns 40 to I += Add then assign It performs addition and then result is assigned to left hand operand I+=J that means I = I + J -= Subtract then assign It performs subtraction and then result is assigned to left hand operand I-=J that means I = I – J *= Multiply the assign It performs multiplication and then result is assigned to left hand operand. I*=J that means I = I * J /= Divide then assign It performs division and then result is assigned to left hand operand I/=J that means I = I / J %= Modulus then assign It performs modulus and then result is assigned to left hand operand I%=J that means I = I % J **= Exponent then assign It performs exponent and then result is assigned to left hand operand I**=J that means I = I ** J //= Floor division then assign It performs floor division and then result is assigned to left hand operand I//=J that means I = I // J
Comparison operator Operator Operator Name Description Example == Equal to If the values of two operands are equal, then it returns true. I = 20, J = 20 (I == J) is True != Not Equal to If the values of two operands are not equal, then it returns true. I = 20, J = 20 (I != J) is False < Less than If the value of left operand is less than the value of right operand, then it returns true I = 40, J = 20 (I < J) is False > Greater than If the value of left operand is greater than the value of right operand, then it returns true I= 40, J = 20 (I > J) is True <= Less than or equal to If the value of left operand is less than or equal to the value of right operand, then it returns true I = 40, J = 20 (I <= J) is False >= Greater than or equal to If the value of left operand is greater than or equal to the value of right operand, then it returns true. I = 40, J = 20 (I >= J) is True <> Not equal to (similar to !=) If values of two operands are not equal, then condition becomes true I=40, J = 20 (I <> J) is True.
• Comparison operator is also known as Relational Operators because it compares the values. After comparison, it returns the Boolean value i.e. either true or false.
Bitwise Operators • It performs bit by bit operation. Suppose there are two variables, I = 10 and J = 20 and their binary values are: I = 10 = 0000 1010 J = 20 = 0001 0100 now let us see how bitwise operators perform.
Operator Operator Name Description Example & Binary AND If both bits are 1 then 1 otherwise 0 I & J 0000 0000 | Binary OR If one of the bit is 1 then 1 otherwise 0 I | J 0001 1110 ^ Binary XOR If both bit are same, then 0 otherwise 1 I ^ J 0001 1110 ~ Binary Complement If bit is 1 the make it 0 and if bit is 0 the make it 1 ~I 1111 0101 << Binary Left Shift The left operands are moved left by the number of bits specified by the right operand. I << 2 240 i.e. 1111 0000 >> Binary Right Shift The left operands are moved right by the number of bits specified by the right operand. I >> 2 15 i.e. 1111
Membership Operators Operator Description Example in It returns true if it finds a variable in the sequence otherwise returns false List = [1,2,3,4,5,6,7,8] i=1 if i in List: print(‘i is available in list’) else: print(‘i is not available in list’) Output – i is available in list not in It returns true if it does not find a variable in the sequence otherwise returns false List = [1,2,3,4,5,6,7,8] j=10 if j not in List: print (‘j is not available in list’) else: print (‘j is available in list’) Output – j is not available in list
Identity Operators • These operators are used to compare the memory address of two objects. Operator Description Example is It returns true if both operand ‘s identity is same otherwise false I = 20 J = 20 if(I is J): print (‘I and J have same identity’) else: print (‘I and J have not same identity’) Output – I and J have same identity is not It returns true if both operand ‘s identity is not same otherwise false I = 20 J = 230 if(I is not J): print (‘I and J have not same identity’) else: print (‘I and J have same identity’) Output – I and J have not same identity
Logical Operators Operator Operator Name Description Example and Logical AND When Both side condition is true the result is true otherwise false 2<1 and 2<3 False or Logical OR When at least one condition is true then result is true otherwise false 2<1 or 2<3 True not Logical NOT Reverse the condition Not (5>4) False
Python Conditional Statements • if statement • if else statement • nested if statement • for loop • while loop
if statement
if else statement
nested if statement
for loop The syntax of for loop is for i in range(n): S1 S2 S3 print() First time i is initialized to zero and all the statements in the body of for loop(i.e. S1,S2,S3) are executed once, then i increased by 1, again all the statements in the body are executed, again i increased by 1 and the process continues ,when i reaches n then the control comes out of for loop and the next statement (in this case it is the print statement) is executed ( I takes the values from 0 to n-1). for i in range(1,n): S1 S2 S3 print() In this case i start with 1and takes the values up to n-1. for i in range(1,n,2): S1 S2 S3 print() In this case i start with 1 and takes the values less than n with increment 2.
while loop The syntax of while loop is while condition: S1 S2 S3 print() The while loop is executed as long as condition is true if the condition is false then the control comes out of the loop and the next statement is executed.
websites • https://intellipaat.com/blog/tutorial/python-tutorial/ • https://www.codinground.com/tokens-python/ • https://datatofish.com/install-python/ • https://www.w3schools.com/python/default.asp • https://realpython.com/ • https://www.programiz.com/python-programming/first- program • https://www.youtube.com/results?search_query=python
An overview on python commands for solving the problems

An overview on python commands for solving the problems

  • 1.
    “Lecture on Python” by Dr.Ravikiran A. Mundewadi Assistant Professor Department of Mathematics M.E.S College of Arts, Commerce and Science, Malleswaram, Bengaluru-03
  • 3.
  • 4.
    • Guido VanRossum is a Dutch programmer best known as the creator of the python programming language. When he began implementing python, Guido Van Rossum was also reading the published scripts from “Monthly Phythan’s Flying Circus, a BBC comedy series from the 1970’s. • Van Rossum thought he needed a name that was short, unique and slightly mysterious, so he decided to call the language Phythan.
  • 5.
    What is Python? •Python is a high-level, general-purpose programming language with an elegant syntax that allows programmers to focus more on problem-solving than on syntax errors. One of the primary goals of Python Developers is keeping it fun to use. Python has become a big buzz in the field of modern software development, infrastructure management, and especially in Data Science and Artificial Intelligence. Most recently, Python has risen to the top 3 list of TIOBE index of language popularity. • Python is becoming increasingly ubiquitous, but you must be wondering why Python has become such a hot topic in the developers’ world. In this tutorial, you will understand all reasons behind Python’s popularity.
  • 6.
    • It isoften used as a “Scripting language ” for web applications. This means that it can automate specific serious of tasks, making it more efficient. Consequently python is often used in software applications, pages within web browser, the shells of operating systems and same games. What is python mainly used for?
  • 7.
    Why is pythonso popular? • More productive. First and foremost reason why python is so much popular is so much popular because it is highly productive as compare to other programming language like C++ and JAVA, etc. Python is also very famous for its simple programming syntax, code readability and English like commands that make coding in python lot easier and efficient.
  • 8.
  • 9.
  • 10.
    Step 1: DownloadPython 3.9.1 • To start, go to python.org/downloads and then click on the button to download the latest version of Python:
  • 11.
    Step 2: Runthe .exe file • Next, run the .exe file that you just downloaded:
  • 12.
    Step 3: InstallPython 3.9.1 • You can now start the installation of Python by clicking on Install Now:
  • 15.
    IDLE: Integrated Developmentand Learning Environment
  • 16.
    OUTPUT: Immediate mode •>>> indicates ‘Prompt’
  • 17.
  • 18.
  • 19.
    • This ishow the syntax would look like in the “untitled” box:
  • 20.
    • Press F5on your keyboard. You will then get the following message to save your code: • Click on ‘OK’ to save the file • Choose a location where the Python file will be saved on your computer. You’ll also need to type a name for your file. For example, type “Test” for your file name:
  • 21.
    Save the FileName • It Save as “Test.py” • “.py” indicates ‘Extension’ • Once you’re done, press Save, and you’ll then see the “Hello World” expression printed on your Python Shell:
  • 22.
    Python Shell: • Itgives the output as “Hello world”
  • 23.
    INSTALLING PACKAGES FORPYTHON • 1. Go to search and type: cmd command prompt or windows powershell is opened • 2. Type: python -m pip install --upgrade pip • 3. pip install numpy • 4. pip install sympy • 5. pip install matplotlib • 6. pip install scipy
  • 24.
  • 25.
  • 26.
  • 29.
    What is PIP? •PIP is a package manager for Python packages. • Pip is one of the most famous and widely used package management system to install and manage software packages written in Python and found in Python Package Index (PyPI). Pip is a recursive acronym that can stand for either "Pip Installs Packages" or "Pip Installs Python". Alternatively, pip stands for "preferred installer program".
  • 30.
  • 31.
    Statements • Python statementsare nothing but logical instructions that interpreter can read and execute. It can be both single and multiline. There are two categories of statements in Python: • Expression Statements • Assignment Statements
  • 32.
  • 33.
  • 38.
  • 39.
  • 41.
  • 43.
  • 44.
    Tokens • Tokens arebuilding blocks of a language. They are the smallest individual unit of a program. There are five types of tokens in Python.
  • 45.
    Keywords • Key words:Key words are pre defined words with special meaning and these are reserved words must not be used as normal identifier names. These key words consists of False, True, None, and, or, not, as, is, in, if, elif, else, for, while, assert, del, break, class, continue, def, except, global, finally, from, import, lambda, non local, pass, return, try, with, yield.
  • 46.
    Identifiers • Identifiers arethe names given to variables, objects, classes, functions, lists and so on. • Variables names must not be key words and must not contain blank spaces. • Variable names are made up of (A-Z, a-z, 0-9, underscore_) and must not start with a digit. Valied variables names are ABCD, abcd, AbCd, A345, a_bG78, _product. • Invalid variable names are 6ab, av fd, for, and.
  • 47.
  • 48.
    String Literals: A stringliteral is a sequence of characters surrounded by quotes. We can use both single, double or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes. Numeric Literals: Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types Integer, Float, and Complex. Boolean Literals: A Boolean literal can have any of the two values: True or False. Collection literals: There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals. Special literals: Python contains one special literal i.e. None. We use it to specify to that field that is not created.
  • 49.
    Punctuators or Separators •‘ indicates single quote • “ indicates double quote • # indicates for comment • indicates backslash • ( ) indicates parentheses • [ ] indicates square brackets • { } indicates braces • : indicates colon • , indicates comma
  • 50.
    Input and OutputStatement • Input statement : input() is used to input string, int(input()) is used to input integer and float(input()) is used to input floating value. • Output statement: • print statement : • a=2 • b=3 • The print statement is a function its general form is • Print(‘this is’,a,’that is’,b) • Where a and b are pre assigned variable names. If the above statement is executed the output will be as , this is 2 that is 3, anything written between two single quotes or between two double quotes are considered as string.
  • 51.
    Lists, Tuple andFunctions Lists: Lists are defined as a= [ 1,2,3,4], indexing start with 0 i.e. a[0]=1,a[1]=2,a[2[=3, a[3]=4. The elements of the list may be any data type and are mutable. Tuple : Tuples are the group of any data types separated by commas and are enclosed in parenthesis.The elements of tuple are immutable. Example: a=(1,2,3), b=( ‘a’,2,”C”). Functions : Functions are defined as def name(arguments): return expression Example: def f(x,y): return x+y+2
  • 52.
  • 53.
    Arithmetic Operators Arithmetic OperatorsOperator Name Description Example + Addition Perform Addition I=40, J=20 >>>I+ J >>>60 - Subtraction Perform Subtraction I=40, J=20 >>>I – J >>>20 * Multiplication Perform Multiplication I=40, J=20 >>>I * J >>> 800 / Division Perform Division I=30, J=20 >>>I /J >>> 2.5 % Modulus Perform remainder after Division I=40, J=20 >>>I /J >>> 0 ** Exponent Performs exponential (power) calculation I=4, J=20 >>>I /J >>> 204 // Floor Division Perform division remove the decimal value and return Quotient value I=30, J=20 >>>I//J >>> 1
  • 54.
    Assignment Operators Operator OperatorName Description Example = Assignment It assigns value from right side operand to left side operand I = 40 It assigns 40 to I += Add then assign It performs addition and then result is assigned to left hand operand I+=J that means I = I + J -= Subtract then assign It performs subtraction and then result is assigned to left hand operand I-=J that means I = I – J *= Multiply the assign It performs multiplication and then result is assigned to left hand operand. I*=J that means I = I * J /= Divide then assign It performs division and then result is assigned to left hand operand I/=J that means I = I / J %= Modulus then assign It performs modulus and then result is assigned to left hand operand I%=J that means I = I % J **= Exponent then assign It performs exponent and then result is assigned to left hand operand I**=J that means I = I ** J //= Floor division then assign It performs floor division and then result is assigned to left hand operand I//=J that means I = I // J
  • 55.
    Comparison operator Operator OperatorName Description Example == Equal to If the values of two operands are equal, then it returns true. I = 20, J = 20 (I == J) is True != Not Equal to If the values of two operands are not equal, then it returns true. I = 20, J = 20 (I != J) is False < Less than If the value of left operand is less than the value of right operand, then it returns true I = 40, J = 20 (I < J) is False > Greater than If the value of left operand is greater than the value of right operand, then it returns true I= 40, J = 20 (I > J) is True <= Less than or equal to If the value of left operand is less than or equal to the value of right operand, then it returns true I = 40, J = 20 (I <= J) is False >= Greater than or equal to If the value of left operand is greater than or equal to the value of right operand, then it returns true. I = 40, J = 20 (I >= J) is True <> Not equal to (similar to !=) If values of two operands are not equal, then condition becomes true I=40, J = 20 (I <> J) is True.
  • 56.
    • Comparison operatoris also known as Relational Operators because it compares the values. After comparison, it returns the Boolean value i.e. either true or false.
  • 57.
    Bitwise Operators • Itperforms bit by bit operation. Suppose there are two variables, I = 10 and J = 20 and their binary values are: I = 10 = 0000 1010 J = 20 = 0001 0100 now let us see how bitwise operators perform.
  • 58.
    Operator Operator NameDescription Example & Binary AND If both bits are 1 then 1 otherwise 0 I & J 0000 0000 | Binary OR If one of the bit is 1 then 1 otherwise 0 I | J 0001 1110 ^ Binary XOR If both bit are same, then 0 otherwise 1 I ^ J 0001 1110 ~ Binary Complement If bit is 1 the make it 0 and if bit is 0 the make it 1 ~I 1111 0101 << Binary Left Shift The left operands are moved left by the number of bits specified by the right operand. I << 2 240 i.e. 1111 0000 >> Binary Right Shift The left operands are moved right by the number of bits specified by the right operand. I >> 2 15 i.e. 1111
  • 59.
    Membership Operators Operator DescriptionExample in It returns true if it finds a variable in the sequence otherwise returns false List = [1,2,3,4,5,6,7,8] i=1 if i in List: print(‘i is available in list’) else: print(‘i is not available in list’) Output – i is available in list not in It returns true if it does not find a variable in the sequence otherwise returns false List = [1,2,3,4,5,6,7,8] j=10 if j not in List: print (‘j is not available in list’) else: print (‘j is available in list’) Output – j is not available in list
  • 60.
    Identity Operators • Theseoperators are used to compare the memory address of two objects. Operator Description Example is It returns true if both operand ‘s identity is same otherwise false I = 20 J = 20 if(I is J): print (‘I and J have same identity’) else: print (‘I and J have not same identity’) Output – I and J have same identity is not It returns true if both operand ‘s identity is not same otherwise false I = 20 J = 230 if(I is not J): print (‘I and J have not same identity’) else: print (‘I and J have same identity’) Output – I and J have not same identity
  • 61.
    Logical Operators Operator OperatorName Description Example and Logical AND When Both side condition is true the result is true otherwise false 2<1 and 2<3 False or Logical OR When at least one condition is true then result is true otherwise false 2<1 or 2<3 True not Logical NOT Reverse the condition Not (5>4) False
  • 62.
    Python Conditional Statements •if statement • if else statement • nested if statement • for loop • while loop
  • 63.
  • 64.
  • 65.
  • 66.
    for loop The syntaxof for loop is for i in range(n): S1 S2 S3 print() First time i is initialized to zero and all the statements in the body of for loop(i.e. S1,S2,S3) are executed once, then i increased by 1, again all the statements in the body are executed, again i increased by 1 and the process continues ,when i reaches n then the control comes out of for loop and the next statement (in this case it is the print statement) is executed ( I takes the values from 0 to n-1). for i in range(1,n): S1 S2 S3 print() In this case i start with 1and takes the values up to n-1. for i in range(1,n,2): S1 S2 S3 print() In this case i start with 1 and takes the values less than n with increment 2.
  • 67.
    while loop The syntaxof while loop is while condition: S1 S2 S3 print() The while loop is executed as long as condition is true if the condition is false then the control comes out of the loop and the next statement is executed.
  • 68.
    websites • https://intellipaat.com/blog/tutorial/python-tutorial/ • https://www.codinground.com/tokens-python/ •https://datatofish.com/install-python/ • https://www.w3schools.com/python/default.asp • https://realpython.com/ • https://www.programiz.com/python-programming/first- program • https://www.youtube.com/results?search_query=python