-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathfloats.py
82 lines (62 loc) · 3.13 KB
/
floats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -----------------------------------------------------
# Tutorial: An explanation of the float datatype
# -----------------------------------------------------
# In python, decimals are stored using the float datatype
number1 = 10.10 # We can directly assign a decimal to a variable
# We can check the datatype of a variable using the type function. So let's verify if the variable number1 is a float or not
print("Type of number1 is", type(number1))
# We can see that Python is smart enough to make the variable as a float. This is called Dynamically typed.
# We can also declare a decimal using the float() function
number2 = float(20.20)
# We can see that the variable number2 is a float too. This is called Static typed as we are giving the type before itself
print("Type of number2 is", type(number2))
# We can also convert from other datatype to decimal using the float()
number3 = float("30.30")
# Python converts the string to a float.
print("Type of number3 is", type(number3))
number3 = float(30)
# Python converts the int to a float.
print("Type of number3 is", type(number3))
# Floats can be negative decimals too
number4 = -10.10
print("Type of number4 is", type(number4))
# Operations on float
# Addition: Two floats can be added using the + sign
sum = number1 + number2
print(f'{number1} + {number2} is {sum}')
# Subtraction: Floats can be subtracted using the - sign
diff = number3 - number2
print(f'{number3} - {number2} is {diff}')
# Multiplication: Two floats can be multiplied using the * sign
mul = number1 * number2
print(f'{number1} * {number2} is {mul}')
# Division: Two integers can be divided using the / sign
div = number3 / number1
# Remember the answer is a float too
print(f'{number3} / {number1} is {div}')
# Remainder: The remainder left when a number is divided by another number can be found using % sign
rem = number3 % 11
# Gives the remainder when number3 is divided by 11
print(f'{number3} % 11 is {rem}')
# Power: A float can be raised to a power using ** operator
pow = number1 ** 2
# Returns number1 to the power of 2
print(f'{number1} to the power 2 is {pow}')
# Printing and formatting floats
# The number of decimals positions printed while printing a float can be controlled in python
number5 = 1.23456789
# We can use two methods:
# 1) The newer fstrings (Recommended)
# Format: {variable:.nf} where n is the number of decimals to be printed
print(f"{number5:.2f} is {number5} in two decimals using f-string")
# 2) Using the older format()
# format feeds each of the parameters passed to it into the curly braces sequentially within a string
print("{:.2f} is {} in two decimals using format".format(number5, number5))
# ------------------------------------------------------------------------------------------------------------------
# Challenge 1: Declare two floats and find the sum, difference, product, quotient and remainder of the two numbers
# ------------------------------------------------------------------------------------------------------------------
# Add your code here
# ----------------------------------------
# Challenge 2: Find the square root of 121
# ----------------------------------------
# Add your code here