Lesson 1 Variables and the print() function


Hey there 😊

Thank you for joining my first post in Learning Python Programming and on to a new world of using the language to benefit you as best as possible.

This Lesson will be split into two Points:

  • Variables
  • print() function

What can you expect from this lesson?

In this lesson you will be able to understand how the programming language stores what you want in the form of a variable. Then we will be utilising our first function namely the print() function, which you came across in my previous post here.

Okay enough of that…

Variables:

In my opinion a variable could be considered as a container, something that is able to hold almost anything if not everything 😊. In the Python Programming language, a variable is used exactly as I described to store a certain data type.

Let us pick up where we left off from the environment setup in the previous post (Getting started with Python and the environment setup).


Creating a new script file and saving in our new code location

 

  • Once you have Python IDLE open select file and New file

269c8b85c5b5533d9e0a23e872eeed22d96c74dcf064fcb5a546c682f906c915.jpeg

 

  • A new window should have popped up as untitled let us select file and save as in a new location

8f5ab8c8d0978ed11f689c4cad31a38d72226062eb6cff4d50ce0b2f938bf447.jpeg

  • I have chosen the following location:
    • C:\DEV\DEV

95e484bb0418c80a87d710a75edd278a1a2f7314cbef71bfa697a8ec1b434301.jpeg

  • Once saved we can dive into some code YEAAAAH!!! (My favourite part) as learning is best in a practical sense.

 

Note!! I shall be using code blocks for ease of copying code 😉

 

# The hash symbol is used for comments and will not execute with your code consider it as a note to self.

# Assigning an name to a variable is as simple as putting a name best understood by you
# I believe to stick to a certain standard, for example:
# 	user_name
#	user_passord
# another note a variable cant start with a number ;)


variable_a = 2
variable_b = 6

 

  • Well done, lets ctrl-s (short for file and save) and run our first file by clicking run and run module

d0b44ac1023d5f8d57f3ecfc389b2f5955028fcf010177a38e6e56e591fe433d.jpeg

  • If you ended up with this output :

dca31d9e2f3f5de76a0a80d05f80a46227f74d954389d8bf8c882dfa39fbcd97.jpeg

Then all is working, Yeaaaah....

 

...but MonsterNur there is nothing to see. True but if you did soemthing wrong you would see errors :)

Lets get into the next part where we actually see the variables we have assigned on the screens output after we run it.

 

Using the print() function to print our variables

 

Remember the previous post we used the following line of code to print a sentence to the screen:

print(“Hello World of Python”)

 

  • Well the “print()” part is actually called a function and its “function” is to print a data type which you place inside its parenthesis, lets check this out next.

Remember I shall be using comments in code blocks for ease of copy paste

# The hash symbol is used for comments and will not execute with your code consider it as a note to self.

# Assigning an name to a variable is as simple as putting a name best understood by you
# I believe to stick to a certain standard, for example:
# 	user_name
#	user_passord
# another note a variable cant start with a number ;)


variable_a = 2
variable_b = 6

# Printing our variables
# simply add the variable assigned above like this:

print(variable_a)
print(variable_b)

  • Lets run this code now with the added print function

86645abb58636a8fd5ac67293173831b71926030ab2274877e0bc0f43d258018.jpeg

 

Well done !!!

that is how easy it can be to start writing code, storing a value in a variable and then printing the variable on request using the print() function.

I think that quick guide will get you started with ease.

Remember make sure to follow so you can check in daily for new content on our journey to learning Python Programming Language.

How do you rate this article?

4


MonsterNur
MonsterNur

I believe myself to be an artist in software solution innovation, my preferred tool in my toolbox is the Python Programming Language and I believe in one thing: Nothing is impossible.


Monsternur and the world of Python
Monsternur and the world of Python

If you are looking to add a tool to your ever growing toolbox, then this is the place to be. The tool I will be teaching you to use is PYTHON, Lets have fun

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.