Working with Comments, Variables and Operators in Python

Working with Comments, Variables and Operators in Python

In the previous article, we wrote our first program using the print() statement. We also learnt about text editors, IDE’s and how to install python. If you haven’t read that, check it out here .

The print() statement makes us able to communicate with the user.

Input:

print ("Hey User!")

Output:

Hey User!

Very soon, you’ll get to write complex codes that not just you might be reading, other programmers might want to borrow your code, you could be working with a team or you might share your codes for other programmers. How then do you communicate or pass instructions to other programmers reading your code in your absence?

Comments

You can write instructions for other programmers checking out your code through comments (Definitely not the type of comments on Instagram or Facebook). Comments are written in your code but they don’t get to be displayed to the user. Comments can even be used for much more…

I once asked a colleague out for a drink via comments. And yeah it worked out well.

Input:

print ("Hey, my name is Python!") 
# I just printed out my name

Output:

Hey, my name is Python!

Python ran through the code, discovered the print() statement, worked with it, moved to the next line but discarded the line because it was a comment.

When a line starts with # , the whole line is considered a comment in python. But then you could be wondering, what if the # isn’t at the beginning of the line?

Input:

print ("Hey, my name is Python!") # I just printed out my name

Output:

Hey, my name is Python!

Python considered everything on the right side of the ‘#’ till the end of the line as a comment. But as much as we can make single line comments, we can also make multiple lines comments.

Input:

print ("I’m working with multiple lines comment!") 
''' This is the first line of the comment! 
This is the second line of the comment! 
This is the third line of the comment! '''

Output:

I’m working with multiple lines comment!

Multiple lines comments are initiated by the use of three single quotation marks (''').

Variables

Right here I’m supposed to tell you that programming sometimes works like mathematics but not freak you out in the process. A lot of people dislike mathematics and if helps to know, I’m amongst this group. Okay, don’t talk to me about this in person. The point is that we’ll only be working with basic maths if it’s ever necessary (And you won’t have to find your ‘x’ like you always tried to do in maths class).

Remember back then in maths class when your teacher always put x in place of an unknown. We do something of that sort in Python. We represent known and unknown values with variables.

UserAge = 56

Above we defined a variable UserAge, our program will allocate a certain area of our computer to store this data. We can then access and modify this data by referring to it by it’s name, UserAge.

Input:

UserAge = 56 
print (UserAge)

Output:

56

Every time you declare a new variable, you need to give it an initial value. In the example above, we gave it a value 56. We can always change this value in our program later.

We can also define multiple variables at one go. To do that simply write:

UserAge, UserName = 20, ‘Benjamin’

This is equivalent to:

UserAge = 30 
UserName = ‘Peter’

Naming a Variable

There are ground rules in writing variable names in Python. A variable name in Python can only contain letters (a-z, A-Z), numbers or underscores (_). However, the first character cannot be a number. You can name your variables classAttendees, class_attendees or ClassAttendees3 but not 3classAttendee. Also, there are some reserved words that we cannot use as a variable name because they already have preassigned meanings in Python. Examples of these words are while, input, print, if, and etc. We’ll learn about most of them later on.

A lot of beginners always have a recurring problem working with python because of one important rule on variables in Python: Variable names are case sensitive. classattendants is not the same as classAttendants.

The Assignment Sign

Now you must know that the = sign in the statement UserAge = 56 has a different meaning from the = sign you worked with in maths class. In programming, the = sign is known as an assignment sign. It means we are assigning the value on the right side of the = sign to the variable on the left. A better way to understand the statement UserAge = 56 is to think of it as UserAge <--- 56.

The statements x = y and y = x have very different meanings in programming.

Now you’re probably wondering that if the = sign isn’t ‘equal to’ in Python, then what’s is the equal to sign?

x == 5

The == sign in python means ‘equal to’.

Basic Operators

Aside assigning a variable an initial value, we can perform the usual mathematical operators in Python include +, -, *, /, //, % and ** which represent addition, subtraction, multiplication, division, floor division, modulus and exponent respectively.

Example: Suppose x = 5, y = 2

Addition: x + y = 7

Subtraction: x - y = 3

Multiplication: x * y = 10

Division: x / y = 2.5

Floor Division: x // y = 2 (rounds down the answer to the nearest whole number)

Modulus: x % y = 1 (gives the remainder when 5 is divided by 2)

Variables and operators in python are so important that it is impossible to skip it and not be confused. With this added knowledge, we’ll wrap up to learn about “Data Types in Python” in the next article.

Subscribe to my newsletter above to be informed every time I drop interesting content. Peace.