Posts tagged Variable

How to create a variable in Python

Hello there, in this post we are going to learning about creating a variable in Python. The process is very simple like creating a variable in other languages but if you are new to programming don’t worry I’ll explain each step in details.

What is Python variable:

A Python variable is used to store some information/value that we can use in our program. It can be anything; a number, string, function, or a variable itself.

Well, this is how a Python variable look like:

website = "Pythonologia"

Let me explain you what just happened above.

The “website”word called as variable name and “Pythonologia” in strings (“double quotation marks”) called as string.

Python variable name: Python variable name can be anything and whatever you want like my name, your name, age, account, and so on. But it can’t be start with numbers like 1website it should start with a alphabet.

Moreover, here are valid types of a Python variable:

  • website1
  • web-site
  • web_site
  • web1site

Invalid types of Python variable:

  • 1website
  • $website
  • website$

I mean other than above mentioned valid types of a variable are invalid.

So, now I am assuming you know what is and how to write variable name. Now, let us look at other part.

The other part called as variable value. It can be anything a number, stings (words), function, and variable itself. Just you need to know how store them correctly.

How to store a string:

A string always entered between the double quotation marks. And it can be number and words. If you store something between quotations than it will become a string.

website = "Pythonlogia"

mynumber = "342"

How to store a number:

A number can be store without quotation marks.

mynumber = 321

How to store a Python variable in Python variable:

You can store like string but without using quotation mark.

newwebsite = oldwebsite

Now, that newwebsitevariable has same value like oldwebsite variable.

I hope you learned how to create a variable in Python. If you have any question or suggestion than feel free to ask in comments.