Advertisement

unit1 python.pptx

Mar. 29, 2023
Advertisement

More Related Content

Advertisement

unit1 python.pptx

  1. Unit-1 Variables,Expression, and Statements
  2. Python variable • Python Variable is containers which store values. • We do not need to declare variables before using them or declare their type. • A variable is created the moment we first assign a value to it. • A Python variable is a name given to a memory location. It is the basic unit of storage in a program.
  3. Variable Example var="first variable" print(var) • Here var is the variable that stores the string “first variable”. • The value stored in a variable can be changed during program execution. • A Python Variables is only a name given to a memory
  4. Rules for creating variables • A variable name must start with a letter or the underscore character. • A variable name cannot start with a number. • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). • Variable names are case-sensitive (name, Name and NAME are three different variables). • The reserved words(keywords) cannot be used naming the variable.
  5. Multiple assignment • Python allows you to assign a single value to several variables simultaneously. For example − • For example: a = b = c = 1 • Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location.
  6. • Variable – Container to store a value • Keywords – Reserved words in Python • Identifiers – class/function/variable name
  7. Values and types • A value is one of the basic things a program works with, like a letter or a number. • You can print values in Python. See what happens when you run the following code. print(17) print('Hello World!') • These values belong to different types: 17 is an integer, and “Hello World!” is a string, so called because it contains a “string” of letters. You can identify strings
  8. Data Types Primarily there are the following data types in Python: • Integers • Floating point numbers • Strings • Booleans • None NOTE: None indicates nothing.
  9. type() function and Typecasting • type function is used to find the data type of a given variable in Python. • For example: a = 31 type(a) #class<int> b = “31” type(b) #class<str>
  10. Example Program
  11. Typecasting • A number can be converted into a string and vice versa (if possible) • There are many functions to convert one data type into another. • Str(31) # ”31” Integer to string conversion • int(“32”) # 32 String to int conversion
  12. • float(32) #32.0 Integer to float conversion … and so on • Here “31” is a string literal, and 31 is a numeric literal.
  13. Input() function • This function allows the user to take input from the keyboard as a string. Syntax: a = input(“Enter name”) #if a is “student”, the user entered • Note: The output of the input function is always a string even if the number is entered by the user. • Suppose if a user enters 34, then this 34 will automatically convert to “34” string literal.
  14. Instructions • An Instruction is an order/command given to a computer processor by a computer program to perform some mathematical or logical manipulations (calculations). • Each and every line or a sentence in any programming language is called an instruction.
  15. Statement in python • Any Instruction that a python interpreter can execute is called a Statement. • The execution of a statement changes state • Execution of a statement may or may not produces or displays a result value, it only does whatever the statement says. • For Example: a=5;
  16. Keywords in Python • Keywords in Python are reserved words that can not be used as a variable name, function name, or any other identifier. • Code to check keywords list in python:
  17. Operators in Python The following are some common operators in Python: 1. Arithmetic Operators (+, -, *, /, etc.) 2. Assignment Operators (=, +=, -=, etc.) 3. Comparison Operators (==, >=, <=, >, <, !=, etc.) 4. Logical Operators (and, or, not) 5. Identity Operators 6. Membership Operators 7. Bitwise Operators
  18. 1. Arithmetic Operators
  19. Practice Question • Write a program that performs all the arithmatic operation on two numbers.
  20. 2. Assignment Operators
  21. 3. Comparision Operator (Relational Operator)
  22. 4. Logical Operators
  23. Example ( logical or )
  24. Example (logical and)
  25. Example ( logical not )
  26. 5. Identity Operators
  27. Example ( is )
  28. Example ( is not )
  29. 6. Membership Operators
  30. Example ( in )
  31. Example ( not in )
  32. 7. Bitwise Operators
  33. • In Python, bitwise operators are used to performing bitwise calculations on integers. The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators. Then the result is returned in decimal format. • Note: Python bitwise operators work only on integers.
  34. Bitwise AND operator • Returns 1 if both the bits are 1 else 0. • Example: a = 10 = 1010 (Binary) b = 4 = 0100 (Binary) a & b = 1010 & 0100 = 0000 = 0 (Decimal)
  35. Bitwise or operator • Returns 1 if either of the bit is 1 else 0 • Example: a = 10 = 1010 (Binary) b = 4 = 0100 (Binary) a | b = 1010 | 0100 = 1110 = 14 (Decimal)
  36. • Most of the bitwise operators are binary, which means that they expect two operands to work with, typically referred to as the left operand and the right operand. Bitwise NOT (~) is the only unary bitwise operator since it expects just one operand.
  37. Bitwise not operator • Returns one’s complement of the number. • Example: a = 10 = 1010 (Binary) ~a = ~1010 = -(1010 + 1) = -(1011) = -11 (Decimal)
  38. Bitwise xor operator • Returns 1 if one of the bits is 1 and the other is 0 else returns false. • Example: a = 10 = 1010 (Binary) b = 4 = 0100 (Binary) a ^ b = 1010 ^ 0100 = 1110 = 14 (Decimal)
  39. Bitwise right shift • Shifts the bits of the number to the right and fills 0 on voids left( fills 1 in the case of a negative number) as a result. • Gets similar results as of dividing the number with some power of two.
  40. • Example 1: a = 10 = 0000 1010 (Binary) a >> 1 = 0000 0101 = 5 • Example 2: a = -10 = 1111 0110 (Binary) a >> 1 = 1111 1011 = -5
  41. Bitwise left shift • Shifts the bits of the number to the left and fills 0 on voids right as a result. • Gets similar results as of multiplying the number with some power of two.
  42. • Example 1: a = 5 = 0000 0101 (Binary) a << 1 = 0000 1010 = 10 a << 2 = 0001 0100 = 20 • Example 2: b = -10 = 1111 0110 (Binary) b << 1 = 1110 1100 = -20 b << 2 = 1101 1000 = -40
  43. Precedence of Python Operators • Expressions: The combination of values, variables, operators, and function calls is termed as an expression. The Python interpreter can evaluate a valid expression. For Example: 5+7 here 5+7 is an expression. NOTE: There can be multiple operators in one expression as well.
  44. • To evaluate these types of expressions there is a rule of precedence in Python. It guides the order in which these operations are carried out. • For example, multiplication has higher precedence than subtraction. >>> 10 - 4 * 2 2 (result)
  45. • But we can change this order using parentheses () as it has higher precedence than multiplication. • For Example: Parentheses () has higher precedence >>> (10 - 4) * 2 12 (Result) • The operator precedence in Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones).
  46. Associativity of Python Operators • We can see in the above table that more than one operator exists in the same group. These operators have the same precedence. • When two operators have the same precedence, associativity helps to determine the order of operations.
  47. • Associativity is the order in which an expression is evaluated that has multiple operators of the same precedence. Almost all the operators have left-to-right associativity. • For example, multiplication and floor division have the same precedence. Hence, if both of them are present in an expression, the left one is evaluated first.
  48. Example • Left-right associativity print(5 * 2 // 3) # Output: 3 • Shows left-right associativity print(5 * (2 // 3)) # Output: 0
  49. Statement vs Expressions Statement • A statement executes something • Execution of a statement may or may not produces or displays a result value, it only does whatever the statement says. • Every statement can be an expression. • Example: >>> x = 3 >>> print(x) Output: 3 Expressions • An expression evaluates to a value. • Evaluation of an expression always Produces or returns a result value. • Every expression can’t be a statement • Example: >>> a + 16 >>> 20
  50. Commments • Lines in the code that are ignored by the python interpreter during the execution of the program. • Comments enhance the readability of the code and help the programmers to understand the code very carefully.
  51. Types of comments • There are three types of comments in Python – 1. Single line Comments 2. Multiline Comments 3. Docstring Comments
  52. 1. Single line Comment • A single-line comment begins with a hash (#) symbol and is useful in mentioning that the whole line should be considered as a comment until the end of the line. • Example:
  53. Multi-Line Comments • Python does not provide the option for multiline comments. However, there are different ways through which we can write multiline comments. 1. Using Multiple Hashtags (#) 2. Using String Literals
  54. Using Multiple Hastags
  55. Using String Literals • Python ignores the string literals that are not assigned to a variable so we can use these string literals as a comment.
  56. Docstring • Python docstring is the string literals with triple quotes that are appeared right after the function. It is used to associate documentation that has been written with Python modules, functions, classes, and methods. It is added right below the functions, modules, or classes to describe what they do. In Python, the docstring is then made available via the __doc__ attribute.
  57. Example • def multiply(a, b): • """Multiplies the value of a and b""" • return a*b • # Print the docstring of multiply function • print(multiply.__doc__)
  58. Operations on string • Python string is a sequence of Unicode characters that is enclosed in the quotations marks. • we will discuss the in-built function i.e. the functions provided by the Python to operate on strings.
  59. • Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings. • Some of the important functions of strings will be discussed in further slides.
  60. • lower(): Converts all uppercase characters in a string into lowercase • upper(): Converts all lowercase characters in a string into uppercase • title(): Convert string to title case • capitalize(): Converts the first character of the string to a capital (uppercase) letter • startswith(): Returns true if the string starts with the specified value
  61. • count(): Returns the number of occurrences of a substring in the string. • endswith(): Returns true if the string ends with the specified value • find(): Searches the string for a specified value and returns the position of where it was found • index(): Searches the string for a specified value and returns the position of where it was found
  62. • isalnum(): Returns True if all characters in the string are alphanumeric • isalpha(): Returns True if all characters in the string are in the alphabet • islower(): Returns True if all characters in the string are lower case • isupper(): Returns True if all characters in the string are upper case
  63. Example text = 'this is pYthon ClAss' print("nUpdated String after uppercase:") print(text.upper()) print("nUpdated String after lowercase:") print(text.lower()) print("nUpdated String afer titlecase:") print(text.title()) # original string never changes print("nOriginal String") print(text) OUTPUT SCREEN-->
Advertisement