Learn Python Fundamental in 30 Days — Day 2(Numbers)

devops everyday challenge
devops-challenge
Published in
3 min readMay 2, 2017

--

Numbers: Python has various types of numbers

  • Integers: are just whole numbers(positive or negative, eg: 1 or -1 )
  • Floating point: Having decimal point in them(eg: 1.0 or -1.1)
  • Bool: Represent Boolean values True and False(Important when we want to test something)
  • NoneType: Special and has one value, None

We can use type() to see the type of an object

>>> type(1)
<class ‘int’>
>>> type(1.0)
<class ‘float’>
>>> type(True)
<class ‘bool’>
>>> type(None)
<class ‘NoneType’>

Basic Arithmetic Operation

  • Addition
>>> 1+1 
2

NOTE: If we try to add int with float it will result float

>>> 1 + 2.0
3.0
  • Subtraction
>>> 2–1
1
  • Multiplication
>>> 2 * 3
6
  • Division
>>> 3/2 
1.5

NOTE: In case if you are using Python2

>>> 3/2
1

Reason for this unexpected answer is in case of Python2, decimal point is truncated and it’s called classic division while in case of Python3 it’s called true division

We have different ways to fix this problem in Python2

Specify one of the number to be a float

>>> 3.0/2
1.5
>>> 3/2.0
1.5

We can also cast the type of function which basically turns integers into floats.

>>> float(3)/2
1.5

The way we convert integer to float,via the same casting we can convert float to integer

>>> int(2.9)
2

NOTE: Python doesn’t try to convert into nearest integer it just truncate the floating part

Finally, my favorite one, importing from a module called future(via future module we are importing Python3 functions into Python2)

>>> from __future__ import division
>>> 3/2
1.5

NOTE: I will discuss more about modules and function in future chapter so don’t worry if you didn’t get it :-)

  • Modulus Operator : It returns remainder
>>> 5%2
1
  • Powers
>>> 3**2
9

There are some common built-in function

>>> pow(2,4)
16

To return an absolute value

>>> abs(-4)
4

To round a number to a given precision in decimal digits(default 0 digits)

>>> round(1.2345)
1
>>> round(1.2345,2)
1.23

For more info please refer to

Simple Operations

  1. Parentheses used to tell Python to do these operations first

2. Operator precedence without parentheses

  • **
  • *
  • /
  • + and —

execute left to right,as appear in expression

>>> 2*5 + 1
11
>>> 2*(5+1)
12
>>> 2 + 5 * 6 +3
35
>>> (2 + 5) * (6 +3)
63

Variable Assignments

To assign a variable in Python use = sign.

>>> x = 1
>>> x
1
>>> x + 2
3

What we are doing here is creating an object(everything in Python is an object) called x and assign it the number 1

Few point to note

  • Value of x is stored in computer memory
  • = assignment binds name to value
  • Retrieve value associated with name or variable by invoking the name or by typing x
>>> pi = 3.14
>>> radius = 1.1
>>> area = pi *(radius**2)
>>> area
3.7994000000000008

So now question is why we are doing all these

Advantage of this approach

  • We can reuse names instead of values
  • It’s easier to change code later
>>> radius = radius + 2
>>> radius
3.1
  • We can re-bind variables names using new assignment statements
  • Previous values may still stored in memory but lost the handle for it(Python Garbage Collector will take care of the clean job)

Exercise

What should be the output of below mentioned expressions

  1. 1 + 5 -3
  2. 3 * 4.0
  3. — 3
  4. Try to find out the difference in output of 7/3 or 7.0/3 or 7/3.0?
  5. What is the difference in output of (1+2)*3 or 1+2*3
  6. 2**3
  7. If a = 5, what is a = a+1
  8. If a = 1 what is the output of a+b
  9. What is the difference when you try to round round(-1.27,1) vs round(1.27,1)
  10. More tricks with round,check the output round(3.5) vs round(4.5) and try to find it out what exactly happen?
  11. More tricks with round, check the output of round(1234567,-1) vs round(1234567,-2) vs round(1234567,-3)

Solution for all these problem available on Day3, for the time being keep trying :-)

Answers: https://github.com/lakhera2016/Learn-Python-Fundamental-in-30-Days/tree/master/Day2-Numbers

So this end of Day2, In case if you are facing any issue, this is the link to Python Slack channel https://devops-myworld.slack.com

Please send me your details

  • First name
  • Last name
  • Email address

to devops.everyday.challenge@gmail.com, so that I will add you to this slack channel

--

--