python

Using Variables In Python

Variables are used to store content. With these variables, you can then do further actions. But what is a variable? Variables are like a cabinet with lots of small drawers. Only 1 content can be put in each drawer. If new content is put in a drawer, the old content falls out.

Our virtual cabinet has a number of drawers. Now, of course, we have to somehow tell Python which drawer we want to use. To select a particular drawer, the “third drawer from the right and second from the top” variant is a little awkward.

So we need a system that is more manageable and understandable. Hence we can name our drawers. For example, we can label a drawer with “first name”. So we know that we will find the first name in this drawer.


When assigning names for our variables, the following criteria apply: No spaces
 
 
In order to use the drawer, i.e. to store something in the drawer (variables), the variable name is assigned a content using an equal sign.

firstname = 'Jean'

It is important that the content (here the name ‘Jean’) is enclosed in single quotation marks.

Double quotation marks can also be used – what the difference is, we will explain later.

firstname = "Jean"

This enclosing is important for two reasons, because the variable content can also consist of several words. Thus, our single quotes enclose our content. If we would do completely without quotation marks, we would assign the content of the second variable to one variable. Technically this works without problems – only if it is not desired, then irritating results come!

firstname = Jean

It does not work. We get an error message “NameError: name ‘Jean’ is not defined”. Why? Here we are trying to assign the content of the variable with the name “Jean” to the variable firstname. This is why this error message occurs.

Would work:

Jean = "My First Name"
firstname = Jean
mcq-pythonPython MCQ and Answers – ListsThis collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Lists”.   1. Which of the following…Read More
 
We can also assign empty content:

firstname = ''

The example looks confusing, of course, as 2 consecutive single quotation marks look like 1 double quotation mark. The double quotation mark spelling looks funny too.

firstname = ""

But what if we need the single quotation mark as content? So words with an apostrophe. As an example of this, “It’s so funny”. But if we wanted to use the “It’s so funny” in a variable:

myvar = 'It's so funny'

Then this raises an error! Why. Python looks for the initial quotation mark and then starts assigning everything to the variable after that up to the next single quotation mark. In our case, it would be “It”. Python is now completely confused with the rest and no longer knows what to do and throws us with an error message: “SyntaxError: invalid syntax”

In order not to experience a catastrophe with the apostrophe, we have to “camouflage” it. In programming languages, this is called “masking”. A backslash is packed in front of this character and the world is all right again for Python

myvar = 'It\'s so funny'
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *