- 1. PE1 Module 2 Python Fundamentals
- 2. 2 Introduction Python consists of a number of coding basics. What do you understand from the below code? It may look like a foreign language. To make meaningful sentences in a foreign language, you must learn its alphabet, words, and grammar. The same is true for a programming language. Python is a programming language. To write meaningful programs, you must learn the programming language’s semantic, syntax rules and basic elements. The syntax rules determine which instructions are legal. The semantic rules determine the meaning of the instructions. Basic elements are the building blocks to write the instructions.
- 3. 3 Python Program Basics Basic elements/fundamentals to create a python program: Variables Identifiers Keywords Datatypes Input/Output Comments Constants Operators
- 4. 4 Variables Variables are containers for storing data values.
- 5. 5 Identifiers An identifier is a name given to a variable. Python has some rules about how identifiers can be formed Every identifier must begin with a letter or underscore, which may be followed by any sequence of letters, digits, or underscores. first conversion payRate counter1 Legal employee salary hello! one+two #second Illegal: Explain why they are illegal?
- 6. 6 Identifiers Identifiers are case-sensitive >>> x = 10 >>> X = 5.7 >>> print(x) 10 >>> print(X) 5.7
- 7. 7 Keywords Some identifiers are part of Python itself (they are called reserved words or keywords) and cannot be used by programmers as ordinary identifiers False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Python Keywords
- 8. 8 Datatypes A data type represents the type of data stored into a variable or memory. Python assumes the type of a variable based on the assigned value.
- 9. 9 Two types of Datatypes 1. Built- in data types: The datatypes which are already available in Python language: =>our focus 2. User-defined data types: The datatypes which are created by programmers.
- 10. 10 Major built- in data types: int datatype: represents an integer number. An integer number is a number with out any decimal point. Example a=10 or b=-20 float datatype: represents floating point numbers. A floating number is a number that contains a decimal point. Example a=123.45 or b=-20.25 bool datatype: represents Boolean value. A Boolean value is a value that can be either True or False. str datatype: represents string. A string is a sequence/group of characters. String can be enclosed inside: Single quotes: 'Welcome to Python Programming' Double quotes: "Welcome to Python Programming"
- 11. 11 As a matter of fact, we can do various kinds of conversions between strings, integers and floats using the built-in int, float, and str types. Datatype Conversion >>> x = 10 >>> float(x) 10.0 >>> str(x) '10' >>> >>> y = "20" >>> float(y) 20.0 >>> int(y) 20 >>> >>> z = 30.0 >>> int(z) 30 >>> str(z) '30.0' >>> integer float integer string string float string integer float integer float string
- 13. 13 Input/Output in Python You can display program data to the console in Python with print(). The input() function pauses program execution to allow the user to type in a line of input from the keyboard. Python takes all the input as a string input by default.
- 16. 16 Input/Output
- 17. 17 Input/Output
- 18. 18 Python Comments Comments are descriptions that help programmers better understand the intent and functionality of the program. Using comments in programs makes our code more understandable. Comments can also be used to ignore some code while testing other blocks of code. Comments are completely ignored by the Python interpreter. Comments can be: Single-Line Comments Multi-Line Comment
- 19. 19 Single-Line Comments in Python In Python, we use the hash symbol # to write a single-line comment.
- 20. 20 Multi-Line Comments in Python We can use # at the beginning of each line of comment on multiple lines. We can use enclosed triple double quotes. We can use enclosed triple single quotes.
- 21. 21 Constants A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers that hold information which cannot be changed later. Python doesn’t have built-in constant types. By convention, Python uses a variable whose name contains all capital letters to define a constant.
- 22. 22 Operators in Python Operators are special symbols used for specific purposes. Python provides many operators for manipulating data. Operators perform an action on one or more operands. Arithmetical Operators Assignment Operators Relational Operators Logical operators Increment and Decrement Operators Bitwise Operators Membership Operators
- 23. 23 Python Arithmetic Operators OperatorDescription Example + Addition - Adds values on either side of the operator 10 + 20 will give 30 - Subtraction - Subtracts right hand operand from left hand operand 10 - 20 will give -10 * Multiplication - Multiplies values on either side of the operator 10 * 20 will give 200 / Division - Divides left hand operand by right hand operand 20 / 10 will give 2 % Modulus - Divides left hand operand by right hand operand and returns remainder 20% 10 will give 0 ** Exponent - Performs exponential (power) calculation on operators 10**20 will give 10 to the power 20 // Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. 9//2 is equal to 4 and 9.0//2.0 is equal to 4.0
- 24. 24 Python Assignment Operators Operator Description Example = Simple assignment operator, Assigns values from right side operands to left side operand c = a + b will assign the value of a + b to c += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a **= Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //= Floor Division and assigns a value, Performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a
- 25. 25 Python Relational Operators Operator Description Example == Checks if the value of two operands are equal or not, if yes then condition becomes true. (10 == 20) is not true. != Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (10!= 20) is true. <> Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (10 <> 20) is true. This is similar to != operator. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (10 > 20) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (10 < 20) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (10>= 20) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a10<= 20) is true.
- 26. 26 Python Logical Operators Operator Description Example and Called Logical AND operator. If both the operands are true then then condition becomes true. (10 and 20) is true. or Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (10 or 0) is true. not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. not(10 and 20) is false.
- 27. 27 Increment and Decrement Operators The increment operator ++ It adds one to a variable. The decrement operator -- It subtracts one from a variable Increment and decrement operators can be used before(prefix) or after(postfix) a variable. In prefix mode (++ variable or - -variable) the operator increments or decrements, then returns the value of the variable. In postfix mode (variable++ or variable- -) the operator returns the current value of the variable, then increments or decrements.
- 28. 28 Post increment Operator The position of the ++ determines when the value is incremented. If the ++ is after the variable, then the incrementing is done last (a post increment). count = 3 amount = 2 * count++ amount gets the value of 2 * 3, which is 6, and then 1 gets added to count. So, after executing the last line, amount is 6 and count is 4.
- 29. 29 Pre increment Operator If the ++ is before the variable, then the incrementing is done first (a pre increment). count = 3 amount = 2 * ++count 1 gets added to count first, then amount gets the value of 2 * 4, which is 8. So, after executing the last line, amount is 8 and count is 4.
- 30. 30 Post decrement Operator The position of the -- determines when the value is decremented. If the -- is after the variable, then the decrementing is done last (a post decrement). count = 3 amount = 2 * count-- amount gets the value of 2 * 3, which is 6, and then 1 gets subtracted from count. So, after executing the last line, amount is 6 and count is 2.
- 31. 31 Pre decrement Operator If the -- is before the variable, then the decrementing is done first (a pre decrement). count = 3 amount = 2 * --count 1 gets subtracted from count first, then amount gets the value of 2 * 2, which is 4. So, after executing the last line, amount is 4 and count is 2.
- 32. 32 Python Bitwise Operators Operator Description Example & Binary AND Operator copies a bit 1 to the result if it exists in both operands. (60& 13) will give 12 which is 0000 1100 | Binary OR Operator copies a bit 1 if it exists in either operand. (60 | 13) will give 61 which is 0011 1101 ^ Binary XOR Operator sets the bit to 1 if the bit of the operand opposite. (60^ 13) will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~60 ) will give -60 which is 1100 0011 << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. 60<< 2 will give 240 which is 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. 60>> 2 will give 15 which is 0000 1111
- 33. 33 Python Membership Operators In addition to the operators discussed previously, Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples. Operator Description Example in Evaluates to true if it finds a variable in the specified sequence and false otherwise. x in y, here in results in a 1 if x is a member of sequence y. not in Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. x not in y, here not in results in a 1 if x is a member of sequence y.
- 34. 34 Python Operators Precedence Operator Description () ++ - - ** Parentheses, increment, decrement, exponentiation ~ complement * / % // Multiply, divide, modulo and floor division + - Addition and subtraction >> << Right and left bitwise shift & Bitwise 'AND' ^ | Bitwise exclusive `OR' and regular `OR' <= < > >= Comparison operators <> == != Equality operators = %= /= //= -= += *= **= Assignment operators is is not Identity operators in not in Membership operators not or and Logical operators