Header Ads

Python Programming Shell & Python Variables Explained

This article explains you about Python Programming Shell and Python Variables types.

In the previous articles, we have explained you the below topics that will help you to understand from python basics.

Continue with the previous article, we will cover the below topics in this article.

1. How to use Python Programming Shell?
2. Types of Python Programming Mode?
3. What is Python Variables?

Python Programming Shell & Python Variables Explained

Python Programming Tutorials - Python Shell, Python Variables

How to use Python Shell?

To access the Python Programming shell, use the command "python" from the terminal as below. This will take us to the interactive shell of Python.

[root@server ~]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Once we are into the Python Programming Shell, we could see some basic information about python like versions, helps, copyright, credits or license.

Python is similar to other programming languages like Perl, C, and Java. However, there are some differences between each languages. But python is so simple and beginner friendly.


Types of Python Programming Modes

In Python, There are two types of programming mode which are,

1. Interactive Mode Programming - It is a Python Interactive Shell, Without calling any python scripts along with python command as an arguement like (python myprogram.py).

2. Script Mode Programming - Use the python program by calling the script to execute along with python command, and the python interpreter will be active until the script is running. When the script is finished, the interpreter is no longer active.

Generally Python Program files are ending with ".py" extensions.

So lets get started with some basic programs. Use any editors to create a simple python program as shown below.

[root@server ~]# vi myprogram.py
print ("Hello World!")

Save and Exit from the file. "Print" is the function helps us to print the strings (words) within the braces and quotes.

Lets execute or call the program as below,

[root@server ~]# python myprogram.py
Hello World!
[root@server ~]#

This is call Python script mode programming. Using the python program by calling the script to execute along with python command.

Normally when you see any promgramming lectures, they always talk about how to print hello world, thats quite common in programming world. because, we always should start with a small programs and functions.

What is Python variables?

When we take any type of programming languages, variables plays a important things in programming language.

What is variables?

Variables are used for storing data in a reserved memory space, something like numbers, decimals or strings. So memory can hold a value to the assigned variable name.

For example, get into the python programming shell.

Lets Create a variable name "color" and assign a value for the variable as "blue".

>>> color = "blue"

Where "color" is the variable name and it holds the value "blue". and equal symbol is used as a operator.

Now, Lets print the value of the declared variable,

Just enter the variable name and hit enter, that will print the value of the variable name "color" as "blue".

>>> color

Lets take an another examples to understand multiple declaration in variables.

There might be a requirement to create a multiple variables in your python project something related to office staffs.

Lets say you have the employees "kingston", "Mohan", "barry" and so on. Generally while declaring the variables, we use the below format.

staff1 = "Kingston"
staff2 = "Mohan"
staff3 = "Barry"

This is how we generally create multiple variables.

In python, we have a option for multiple declaration which is multiple variable name and multiple value in a single line respectively.

for example,

>>> staff1,staff2,staff3 = "Kingston","Mohan","Barry"

Now if we want to get the value of each variable, just give variable name like staff1 or staff2 and hit enter as below.

>>> staff1
>>> staff2
>>> staff3

This is a nice feature we have in python and also quite easy to add a new entry in case of any changes - multiple declaration or multiple assignment in a single line.

Again one more example scenario, lets say we wanted to assign a single value for multiple variables.

Example, To add a department value for each students in general programming languages, we use the below format.

student1 = "CS"
student2 = "CS"
student3 = "CS"

In python, we can do the same in this very format.

>>> student1 = student2 = student3 = "CS"

So multiple variable with a single value. In the last example, we have seen multiple variable declaration with different values. Here multiple variable holds the same value.

Now lets print the value of a variables,

>>> student1
>>> student2
>>> student3

All  variables hold the single value. So simple.

This is how we declare or assign a single value for a multiple variables or multiple values for multiple variables in a single line. python tutorial, python 
Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.
يتم التشغيل بواسطة Blogger.