Title here
Summary here
x = 1
y = 2
z = x + y
print(z)
Making it interactive with input()
x = input ("whats x? ")
y = input ("whats y? ")
z = x + y
print (z)
Any input from keyboard is treated as a string. For input 2 and 2, output will be 22 as + is concatenating both numbers because they are treated as strings.
int
isn’t just a data type but also a function which can convert 2 from str
type to int
type.
int()
can be used to correct by converting them to integers.
x = int(input ("whats x? "))
y = int(input ("whats y? "))
print(x+y)
Argument moves from inner function, becomes input to outer function
print ( int( input("whats x? ")) + int(input("whats y? ")))
One line but very complicated and clever for its own good.
4.1
is a float
float - is any number with a decimal point
(2.0)
.float
and int
will be a float
._
, (1_00_00_000)
python will only print the digits.Round number to a nearest digit
round(number[, ndigits]) # [] code in square brackets means optional
round means just one number, but if more is needed then it can be specified.
Rounding to a nearest number
x = float (input ("whats x? "))
y = float (input("whats y? "))
z = round ( x + y )
print (z)
print ( f" {z:,} " )
# creating a f-string argument and then applying {z:,} makes the number have comma
# there isnt an upper bound on how large an int can be but float can get cut off into finite digits
z = round(x / y, 2)
# 2 in the argument of z allows rounding to nearest 2 decimals
z = x/y
print ( f"{z:.2f}" )
# by converting it into an f-strinf, format string i can round the result decimal to 2