by BehindJava

What are Numbers,Basic Arithmetic Operations,classic division and floor division and Object Assignment in Python with Syntax and Examples.

Home » python » What are Numbers,Basic Arithmetic Operations,classic division and floor division and Object Assignment in Python with Syntax and Examples.

In this tutorial we are going to learn about Numbers in Python with few examples.

We’ll learn about the following topics:

  1. Types of Numbers in Python
  2. Basic Arithmetic
  3. Differences between classic division and floor division
  4. Object Assignment in Python

Types of Numbers

Python has various “types” of numbers . We’ll mainly focus on integers and floating point numbers. Integers are just whole numbers, positive or negative.

For example: 2 and -2 are examples of integers.

Floating point numbers in Python are notable because they have a decimal point in them, or use an exponential (e) to define the number.

For example: 2.0 and -2.1 are examples of floating point numbers. 4E2 (4 times 10 to the power of 2) is also an example of a floating point number in Python.

Throughout this tutorials we will be mainly working with integers or simple float number types.

Here is a table of the two main types we will spend most of our time working with some examples:

Examples:

  • Number - 1,2,-5,1000

    • “Type” - Integers
  • Number - 1.2,-0.5,2e2,3E2

    • “Type” - Floating-point numbers

Basic Arthematic

  • Addition - 2+1=3
  • Subraction - 4-2=2
  • Multiplication - 2*3
  • Division - 4/2
  • Floor Division - 7//2(The // operator (two forward slashes) truncates the decimal without rounding, and returns an integer result).
  • Modulo - 4%8-It gives the remainder of division.
  • powers - 2**4

Variables

Now that we’ve seen how to use numbers in Python as a calculator let’s see how we can assign names and create variables. We use a single equals sign to assign labels to variables. Let’s see a few examples of how we can do this.

Example: a=10,name=john ,x=20 e.t.c;

The names you use when creating these labels need to follow a few rules:

  1. Names can not start with a number.
  2. There can be no spaces in the name, use _ instead.
  3. Can’t use any of these symbols :’”,<>/?|()!@#$%^&*~-+
  4. It’s considered best practice (PEP8) that names are lowercase.
  5. Avoid using the characters ‘l’ (lowercase letter el), ‘O’ (uppercase letter oh), or ‘I’ (uppercase letter eye) as single character variable names.
  6. Avoid using words that have special meaning in Python like “list” and “str”

Dynamic Typing

Python uses dynamic typing, meaning you can reassign variables to different data types. This makes Python very flexible in assigning data types; it differs from other languages that are statically typed.

Example:
myfriends=2
You can reassign it as,
my
friends=[‘sammy’,’tom’]

Pros and Cons of Dynamic Typing

  • Pros of Dynamic Typing

    • very easy to work with
    • faster development time
  • Cons of Dynamic Typing

    • may result in unexpected bugs!
    • you need to be aware of type()

Determining variable type with type():

You can check what type of object is assigned to a variable using Python’s built-in type() function.

Common data types include:

  • int (for integer)
  • float
  • str (for string)
  • list
  • tuple
  • dict (for dictionary)
  • set
  • bool (for Boolean True/False)

Example:
type(1)=int
Type(1.4)=float

Example for variable assigning:
Ex-myincome=200
tax
rate=0.3
mytaxes=myincome*tax_rate