Hey there Steemers, today I have a Python Programming lesson. In today's lesson, we will be learning about variables, what they are and how we can use them.
What is a Variable?
Variables are used to store values that can be manipulated and changed by a computer program. When assigning a Variable, you give it a dataname, which makes it easy to find and make changes. I like to think of variables as little bags that store information. The whole purpose is to uniquely label and store values.Also, once they are declared, you can use them again throughout the scope of declaration for instance local or global.
Assigning names to a Variable
When naming variables for a computer program you have to follow certain rules of the computer language be it python or PHP and it is important that you name your variable so that it is understandable to another person. You should use good habits straight away as you may want to revisit code months or even years down the line.If coding standards exist them please, please follow them.
Declaring a Variable
When declaring a Variable for your computer program, you will possibly use the = symbol for assignment. The name of your Variable will be on the left and the values will be on the right. For example, I will declare the Variable called 'sname' and make it store the string "Bill".
sname = "Bill"
If you want to display that Variable on your computer program in Python, we will type:
sname = "Bill"
print(sname)
Our expected output should say "Jack".
When we want to change the value assigned to the Variable, we will use the input function. This function saves time by having to not change the variable value when we want to. For example, I will prompt the user to input their name:
sname = input("Hello, What is your name? ")
print(sname)
It asks the user for input with a meaningful question to go along with it. Each time you run the code, you can change this variable without having to do it manually.
However, if you want to store different data types such as a number in a Variable, you can. There are several data types in Python Programming:
String - A Line Of Characters (Text)
Integer - A Whole Number
Float - A Decimal Number
When we want to use these types in a Variable, we will set it up like this:
aFloat = 1.62
aInt = 7
aStr = "Hi there. My name is Chris"
Try for yourself!
Lets combine everything that we have learnt in to one awesome program!
For this exercise, I want you to create a short program which asks for the user's name and age.
It should then return the user's name and age along with a friendly message.
sname = input("Hello What is your name? ")
nage = input("And How old are you? ")
print("Hello", sname, "I hope your well ")
print("You are", nage + ". and looking good!")
Thank you for reading in on this simple Python Programming lessons! If you have any questions, feel free to ask me below. Follow me and get notified when I post more brilliant tutorials!
Thanks,
Chris
Hi Chris,
There is a meetup on in Dublin this weekend. It would be great to see some new faces. If your around check out the @steem-dublin site for the details.
Congratulations @chris2017! You have received a personal award!
1 Year on Steemit
Click on the badge to view your Board of Honor.
Congratulations @chris2017! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!