Python Essentials Classroom Seires – 02/Sept/2021

Assignment

  • In python we use = to assign value to a variable Preview
  • We can also assign the values to multiple variables or names Preview
  • Copying Preview
  • Moment of zen:

Choosing Good Variable Names

  • While writing python code, it is recommended to follow the python style guide Refer Here
  • Generally we will have following ways of naming variables
    • Pascal Casing: When you name variables start every word with capital letter
    Product
    ProductList
    ShoppingCartItem
    
    • Snake Casing: every word start with small case and add _ as seperator for two words
    index
    shopping_item
    
  • According to PEP-8 when we create variables follow snake casing.

Numbers

  • Lets start looking at python’s simple built-in data types

    • Booleans (which will have value True or False)
    • Integers
    • Floats
  • Booleans:

    • In python the only values for boolean are True and False
    • In python we also have a special function bool() can convert other Python data types to a boolean
    • Non zero numbers are considered True: Preview
    • zero valued values are considerd false
  • Integers:

    • Integers are whole numbers , no fractions, no decimal points
    • While writing integers by default they are decimal
    x = 5
    
    • Python also supports bases

      • binary
      x = 0b1010
      
      • octal
      x = 0o765
      
      • hexa
      x = 0x10fd
      
    • Integer Operators: Lets see the basic operators

      Operator Description Example Result
      + Addition 5 + 9 14
      subtraction 98 – 10 88
      * multiplication 90 * 10 900
      / Floating point division 5/4 1.25
      // Integer division 5/4 1
      % Modulus (remainder) 8%5 3
      ** Exponentiation 5 ** 3 125
    • Some examples Preview

    • Operator Precedence:

      • Consider this expression x = 2 + 3 * 4 ** 2
      • what will be order of operations executed?
      • To solve this python has a strict operator precedence Preview
      • Using the operator precedence the expressions will be evaluated as shown below
      2 + 3 * 4 ** 2
      2 + 3 * 16
      2 + 48
      50
      
      • If you want to increase the precedence using parantheses
      2 + (3 * 4 )**2
      2 + 12 ** 2
      2 + 144
      146
      
    • Type Conversions: In python we have special function called as int() which can convert other data types into integer Preview Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin