FAQ: Learn Python: Syntax - Review

This community-built FAQ covers the “Review” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: What is a solution example for the review?

Hi I would like to add a variable inside a string as below:

my_age = 40
greeting = "Hi "
name = "Jack "
age = ", I am "*my_age*" years old!"
greeting_with_name = greeting + name + age
print(greeting_with_name)

I would like to add the my_age variable in the middle of the string for the age variable. But this code comes up with an error.

Thanks for any help

1 Like

It’s concatenation so you should use the ‘+’ sign!

1 Like

Is it possible to use print multiple times? If not, why is that the case?

Hi,
How do I get half_my_age to be a whole number in the code below?

my_age = 48

half_my_age = my_age / 2

greeting = “Hi”

name = “Peter”

greeting_with_name = greeting + " " + name +"!"

print(greeting_with_name)

print(“I am” + " " + str(half_my_age) + " "+ “years old.” + " ")

1 Like

That is a float so how do we convert a float to an integer?
Safest way to convert float to integer in python?

1 Like

This is what I used to get to move on. Side note…I actully put greeting and name in quotes, wrong output, but it still passed me

my_age = 41

half_my_age = 21.5

greeting = ("Whats up, my name is ")

name = (‘Your Name’)

greeting_with_name = (greeting + name)

print(greeting_with_name)

print(my_age)

print(half_my_age)

After looking at yours I want to take mine down, lol. Nice work , I forgot all about the str().

1 Like

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.

Examples

Python 2

>>> 5 // 2
2
>>> 5.0 // 2.0
2

Python 3

>>> 5 // 2
2
>>> 5.0 // 2.0
2

how come

my_age = 23

half_my_age = 23**.05

greeting = “hallo”

name = “charlie steffens”

greeting_with_name = greeting, name

print(greeting_with_name)
comes out as (‘hallo’, ‘charlie steffens’)

but if i use greeting_with_name = greeting + name
it comes out as hallocharlie steffens

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.

Are you only supposed to use quotations in a variable when it is a word, and not when it is a number?

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)-

Or from the actual Python docs (more complete but more technical)-
https://docs.python.org/3/library/stdtypes.html

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.

Thanks

nevermind! I’ve realised I can’t just put my_age = 25, since turned that to the sum my_age 5**2 and I passed fine!

Hi,
I tried string concatenation as follows and got wired results:

greeting = “Hello”

name = “Shriya”

greeting_with_name = greeting, ’ ', name

print(greeting_with_name)

Results:

(‘Hello’, ’ ', ‘Shriya’)

But when I used ‘+’ instead of comma, the results were as expected. Can you please explain what I did wring?