i dont understand why a float number was returned for my_age = 45 …half_my_age = my_age /2
prints = 22.5
i thought you had to specifically specify to return a float number or you would only get a rounded integer?
If you return my_age then it in itself is a float. 45 / 2 = 22.5. It’s doing just that. If you want to round it then one way is to cast it as an integer.
i dont understand. ‘1’ is an integer. 1.2 is a float. if my age is 45, an integer, why does it return a float number. thanks for answering ! i’ll get it, im just a little slow and thick. my whole life has avoided logic ,math and numbers lol
In Python 3, an expression with the division operator (/) will return a float if the quotient is a float. It’s as simple as that — no matter if the dividend and divisors are integers or floats.
>>> 5 / 2
2.5
You may be confused if you learnt Python 2. In Python 2, the same operator (/) will return an integer assuming both the dividend and divisor are integers.
>>> 5 / 2
2
In both Python 2 and 3, you can use integer division (//) to return an integer no matter whether the dividend and divisor are integers or floats.
They are two different kinds of syntax and they ask the computer to do something different. The second option with the + operator is a way to concatenate two strings. So the output is just a new single string consisting of the content of those two strings joined together.
Using a comma as you did with the first option on the other hand will end up producing a tuple which is the most basic kind of sequence in Python. So you wind up with a tuple consisting of two elements which are those two original strings in order.
The quotations, " and ' are used when you specifically want to create a string. So for x to be an in integer type for example, use x = 3 whereas if you want the string type use x = "3".
If you wanted some more background this seems like a decent introduction (but it may cover things you haven’t yet in your lessons)-
Hey I’m not exactly sure how to pass this because to me it’s not exactly clear what I need to do? Offers no solution either so I’ve just assumed that I need to concatenate all the variables into one string.
Its asking:
Assign values to each using your knowledge of division and concatenation!
Which I have done like so:
my_age = 25
half_my_age = my_age / 2
greeting = “Hi there, I’m”
name = “Zacc”
greeting_with_name = greeting + " " + name
print(greeting_with_name + " and I am " + str(my_age) + " years old!")
the only thing I haven’t added is half_my_age but I don’t see how it’s really relevant to a greeting and even though it prints the outcome I get a “EOL while scanning string literal (” error near the Run button but it looks like everything is formatted correctly, any tips? Feel like I must be glossing over something really obvious here but I don’t see it.