Python Programming - Lesson 1 (Variables)

in #howto8 years ago (edited)

Hey there Steemers, today I have another Python Programming lesson. In today's lesson, we will be learning about variables and how we can use them.

What is a Variable?

Variables are used to store information that can be changed/manipulated in a computer program. When assigning a Variable, you give it a name, which makes it easy to find and append changes. I like to think of variables as tiny pigeon holes that store information. The whole purpose is to label and store information. Also, once they are declared, you can use them again throughout your program.

Assigning names to a Variable

Naming variables for a computer program can be quite hard; you have to follow certain rules and it is ideal that you name your variable so that it is understandable to another reader. You should get into good habits straight away as you may want to revisit code months or years down the line.

Declaring a Variable

When declaring a Variable for your computer program, you will use the = symbol. 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 'name' and make it store the string "Jack".

name = "Jack"

If you want to display that Variable on your computer program in Python, we will type:

name = "Jack"
print(name)

Our expected output should say "Jack".

If we want to change the value assigned to the Variable, we can 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:

name = input("What is your name? ")
print(name)

It asks the user for input with a 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 a number in a Variable, you can. There a several data types in Python Programming:

  • Float - A Decimal Number
  • Integer - A Whole Number
  • String - A Line Of Characters (Text)

When we want to use these types in a Variable, we will set it up like this:

myFloat = 3.52
myInt = 14
myStr = "Hi there. My name is Jack."

Try for yourself!

I want you to create a program which asks for the user's name and age. It should return the user's name and age along with a message.

Lets combine everything that we have learnt in to one cool program!

name = input("What is your name? ")
age = input("How old are you? ")
print("Hello", name, "Python is great isn't it?")
print("You are", age + ". Still looking good!")

Thank you for reading in on my Python Programming lessons! If you have any questions, feel free to ask below. Follow me and get notified when I post my latest tutorials!

Thanks,

Jack 

Sort:  

Thanks for the information, I'm learning Python programming I have been going through the Python Tutorial on the SoloLearn App, the community is super helpful there.

i also have just started learning python http://www.python-course.eu/ has been a very useful resource along with the seemit community and of course youtube ..... the most helpful advice that i follow and can share is force yourself to do half hour or a whole hour a day :) hang in there