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.
- Types of Numbers in Python
- Basic Arithmetic
- Differences between classic division and floor division
- Object Assignment in Python
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:
Number - 1,2,-5,1000
- “Type” - Integers
Number - 1.2,-0.5,2e2,3E2
- “Type” - Floating-point numbers
- 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
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:
- Names can not start with a number.
- There can be no spaces in the name, use _ instead.
- Can’t use any of these symbols :’”,<>/?|()!@#$%^&*~-+
- It’s considered best practice (PEP8) that names are lowercase.
- Avoid using the characters ‘l’ (lowercase letter el), ‘O’ (uppercase letter oh), or ‘I’ (uppercase letter eye) as single character variable names.
- Avoid using words that have special meaning in Python like “list” and “str”
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.
You can reassign it as,
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)
- str (for string)
- dict (for dictionary)
- bool (for Boolean True/False)
Example for variable assigning: