Python Programming For Beginners Lesson 2 - Variables

in #programming7 years ago


Introduction

Hello Everyone and welcome to the second lesson of a new series about the Python language. I have just finished a series on the Java programming language which can be found here https://steemit.com/programming/@robertlyon/java-programming-for-beginners-course.

The first lesson of Python Programming for Beginners can be found here https://steemit.com/programming/@robertlyon/python-programming-for-beginners-lesson-1-installing-ide-hello-world.

In this lesson I will be showing you how variables work in Python, if you are familiar with another programming language you may be surprised at the simplicity of declaring variables in Python.

So let's dive straight in.


Variables

Declaring a variable

A variable in programming is similar to a variable in mathematics, for example, if you have the placeholder x for an equation. The letter x in this example could refer to any number and it is known as a variable as it can change depending on the value you wish to use in the equation.

In computer science, a variable is a value that is held in the computers RAM when the programming is running. For example, if I wanted to hold the number 42 in computer memory I would create a statement like number = 42. This Python statement tells the computer that I want to store the variable 42 in memory and if I ever want to access this number I will use the word number(the name of the variable is known as the identifier). An example of this would be print(number). You have seen the print statement in the last lesson and you should be able to deduce that if we combine both of the above statements we will get the output "42" printed to the screen.

Have a look at the images below to see if that is in fact the case. Try this program out for yourself.



Figure 1. Variables example 1


As you can see above when the program is run the number 42 is printed to the screen. This shows that the identifier "number" does in fact hold the value 42.

Types of Variable

We are now going to focus on a couple of different data types in Python. This will give you the tools to store different types of data in variables.

integer

The integer data type is concerned only with whole numbers just like the number 42 that we used in the example above.

float

The float data type deals with floating point numbers such as 3.14, these are numbers with decimal points that contain fractions of numbers.

String

The String data type deals with strings of letters such as "My name is Robert" or the "Hello World" example from the first lesson.

Have a look below for an example of all of these data types being stored into variables and printed to the screen. Try this program out for yourself and create your own values for your variables.



Figure 2. Variables example 2


Conclusion

This lesson was a very simple introduction on how to create a variable and a short introduction to the most common data types used in Python. Hopefully, now you have an understanding of the three data types discussed and you are able to create variables using each of these.

As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.

Thank you for reading and I hope that someone will get some use out of these tutorials.

Message to readers

Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, content is a little sparse at the moment but I am making an effort to provide good quality original content to the Steemit community.

Sort:  

This post received a 77% upvote from @morwhale team thanks to @robertlyon! For more information, click here! , TeamMorocco! .
STEEM Price : 2.693 $

This post has received a 4.00 % upvote from @buildawhale thanks to: @robertlyon. Send at least 1 SBD to @buildawhale with a post link in the memo field for a portion of the next vote.

To support our daily curation initiative, please vote on my owner, @themarkymark, as a Steem Witness

You got a 2.69% upvote from @upme requested by: @robertlyon.
Send at least 1.5 SBD to @upme with a post link in the memo field to receive upvote next round.
To support our activity, please vote for my master @suggeelson, as a STEEM Witness

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 96907.08
ETH 3380.66
USDT 1.00
SBD 3.23