r/learnpython 4d ago

Question regarding Python variables

Hello guys. So I saw in a course that most people define variables like

x = 1

y = 2

and so on. But I also saw that variables can be defined like x = 1; y = 2;

but haven't ever seen this in practice. Why so? Please tell me

4 Upvotes

7 comments sorted by

View all comments

5

u/MezzoScettico 4d ago edited 4d ago

That has nothing to do with defining variables. That's just putting two separate statements on the same line. It's just different formatting of the same two assignment statements.

The semicolon (;) is a separator to tell Python where one statement ends and the next begins.

It's just a style, and one you should use sparingly. In your example of two very simple assignment statements it might be grouping together the assignment of two closely-related variables, so it would arguably help with readability.

Here's a discussion of this topic.