Python- Numbers(Review)



Good Morning boys.

Link for today's Class:
    Meeting ID

    Today is the practical class. I hope your practice sessions for Python are continuing at home. Keep the good work going. Today is the day, don't wait.
    _____________________________________________________________________________

    By the end of this session, you will be able understand and apply the following concepts:
    • What are variable?
    • Ways of assigning  variables
    • Concepts of multiple assignments
    • Introduction to data types in Python (Just introduction)
    • Numeric data type - types and exact difference       
    _______________________________________________________________________________                                                     PYTHON VARIABLES
    Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
    Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.

    Assigning Values to Variables

    Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
    The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example −
    (LIVE DEMO- Hyperlink to help you understand the working practically)
    #!/usr/bin/python
    
    counter = 100          # An integer assignment
    miles   = 1000.0       # A floating point
    name    = "John"       # A string
    
    print counter
    print miles
    print name
    Here, 100, 1000.0 and "John" are the values assigned to countermiles, and name variables, respectively. This produces the following result −
    100
    1000.0
    John
    

    Multiple Assignment

    Python allows you to assign a single value to several variables simultaneously. 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. You can also assign multiple objects to multiple variables. For example −
    a,b,c = 1,2,"john"
    
    Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "john" is assigned to the variable c.
    SO, effectively the above SINGLE statement is assigning three variable a,b,c to the following values :
    a=1
    b=2
    c="john"

    Standard Data Types

    The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.
    Python has five standard data types −
    • Numbers
    • String
    • List
    • Tuple
    • Dictionary

    ___________________________________________________

    Python Numbers

    There are three numeric types in Python:
    • int
    • float
    • complex
    Variables of numeric types are created when you assign a value to them:

    Example

    x = 1    # int
    y = 2.8  # float
    z = 1j   # complex
    To verify the type of any object in Python, use the type() function:

    Example

    print(type(x))
    print(type(y))
    print(type(z))
    Try it Yourself »

    Int

    Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

    Example

    Integers:
    x = 1
    y = 35656222554887711
    z = -3255522

    print(type(x))
    print(type(y))
    print(type(z))
    Try it Yourself »

    Float

    Float, or "floating point number" is a number, positive or negative, containing one or more decimals.

    Example

    Floats:
    x = 1.10
    y = 1.0
    z = -35.59

    print(type(x))
    print(type(y))
    print(type(z))
    Try it Yourself »
    Float can also be scientific numbers with an "e" to indicate the power of 10.

    Example

    Floats:
    x = 35e3
    y = 12E4
    z = -87.7e100

    print(type(x))
    print(type(y))
    print(type(z))
    Try it Yourself »


    Complex

    Complex numbers are written with a "j" as the imaginary part:

    Example

    Complex:
    x = 3+5j
    y = 5j
    z = -5j

    print(type(x))
    print(type(y))
    print(type(z))
    Try it Yourself »

    Type Conversion

    You can convert from one type to another with the int()float(), and complex() methods. This is also known as TYPECASTING.

    Example

    Convert from one type to another:
    x = 1 # int
    y = 2.8 # float
    z = 1j # complex
    #convert from int to float:a = float(x)

    #convert from float to int:b = int(y)

    #convert from int to complex:c = complex(x)

    print(a)
    print(b)
    print(c)

    print(type(a))
    print(type(b))
    print(type(c))
    Try it Yourself »
    Examples
    Here are some examples of numbers −
    intlongfloatcomplex
    1051924361L0.03.14j
    100-0x19323L15.2045.j
    -7860122L-21.99.322e-36j
    0800xDEFABCECBDAECBFBAEl32.3+e18.876j
    -0490535633629843L-90.-.6545+0J
    -0x260-052318172735L-32.54e1003e+26J
    0x69-4721885298529L70.2-E124.53e-7j
    • Python allows you to use a lowercase l with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
    • A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x and y are the real numbers and j is the imaginary unit.
    • ---------------------------------------------------------------------------------------------------------------------------

    Comments

    Popular posts from this blog

    HOLIDAY HW