Total Blog Views: 59
Blog Status: publish
Created By: neyazansari Created at: 08-13-2021
Tags: python basicprogramming pyhton variable naming variable in python
Variable plays an important role in any programming language. It's a base concept in python too. As the name variable suggests, it's something that can be changed or can be varied and an entity that stores and manipulate data. In this “Python variables: A complete beginner’s guide” blog by shadbox, you’ll learn from scratch about what are variables, their types, and the rules for naming a variable in Python.
x = 100
In the above example, a number 100 has been assign to a variable x. The type of variable depends on the type of data it holds. It can be seen clearly that the variable is holding the value that is a number so the type of an 'x' is a number or an integer.
There are three data types of numbers int, float, and complex. Another data type apart from number is called String which is a collection of characters.
Note: To check the data type of variable, use the type() function.
Take a look at this line of code :
Input(" hey what's your name?")
When you run this, you should be pretty familiar with what happens, right? It will ask for a name and when it gets to run, nothing happens. But behind the scene, this input has now been received by the function. But once the input function runs, nothing happens and there's no way to be able to use it in the future. Well, there is a Variable that come in really handy. If I give the name to this result, then we’ll be able to refer to it later on.
name = input(“What is your name”)
What it does, that it assigns whatever the user typed in as the input to this prompt to a variable called 'name'. Now you can go ahead and write print and run the code, you ll see that the string gets printed and you can use it at any point in your code when needed. It's almost you have saved the data from the action to a ‘name’.
Let's make it easier to understand by the real-time example. You had a phonebook let's say and you jotted down the numbers of people, the next time you look at that, there's no way to know who's number is that, right? In a sense, it's the same thing with the computer. Even though you have inputted the piece of the data, there's no way to be able to refer to data unless one can give it a name. So in a phonebook, we might say that this particular number is associated with the name James. In programming, we call this name James a variable. So one can write something like
james = 0987654321
This means that in the future if you ever need the piece of data, you can refer to it by its name. But just because the variable is set to some particular value, it doesn't mean it can't be changed later on. So to make things simple, let's go ahead and delete the input function and just assign a string to the variable name :
name = "Jack"
Print it and then you ll see Jack printed inside the console because variable 'name' is now referred to as string “Jack” . but if later on, you decide to give this variable a different piece of data to hold, you can easily do so.
name = "Jack" print(name) name = "Jones” print(name)
Even though they are both printing the same variable, first is holding a value Jack. but second, it changed it so that its holding onto the value of another. The output will print both the value at the same time.
Look at the following code:
print ( len ( input ( "What's your name?" ) ) )
if you run this code you’ll get a new piece of data, but instead of having such a long line of code, which can be quite confusing, You can simply use a variable that will hold the value of len function, and then you can use that variable in your code at any point. It was exactly the same thing but now you’ve got all of the pieces of data all associated with a name that we can refer to at any point in code.
If you want, you can actually have multiple words in the name of your variable. For example, if you write ‘username’ you can separate each of the words with an underscore but you can't have a space in between two. The name of a variable should be one single unit and in order to separate words in python, you can use underscore.
if you want to use numbers in the name of your variable, you can. but they can't be at the beginning of the name of the variables. For example, you can’t write ‘1name’ or ‘2name’ that will generate a syntax error as well.
And finally, certain privileged words are used for example the name of a function as input or print and it's usually good practice to not use them as the name of your variables because it's very confusing.
we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link