Python variables

Variables in Python (x,y,z). They can be used later in the program

Variables can hold numbers that you can use one or more times.

Numbers can be of one of these datatypes:

  • integer (1,2,3,4)

  • float (numbers behind the dot)

  • boolean (True or False)

Related Course:
Python Programming Bootcamp: Go from zero to hero

Numeric variables example

Example of numeric variables:

x = 1
y = 1.234
z = True

You can output them to the screen using the print() function.

x = 1
y = 1.234
z = True


Python supports arithmetic operations like addition (+), multiplication (*), division (/) and subtractions (-).

#!/usr/bin/env python

x = 3
y = 8

sum = x + y


More mathematical operations

User input

Python 3
Use the input() function to get text input, convert to a number using int() or float().

#!/usr/bin/env python

x = int(input("Enter x:"))
y = int(input("Enter y:"))

sum = x + y

Python 2 (old version)
You can also ask the user for input using the raw_input function:

#!/usr/bin/env python

x = int(raw_input("Enter x:"))
y = int(raw_input("Enter y:"))

sum = x + y

If you are new to Python programming, I highly recommend this book.

Download Python Exercises


Leave a Reply:

Jacob Davidson Tue, 05 May 2015

In the latest python raw_input() is changed to input() might consider changing or commenting. Thanks for the site it is a huge help

Frank Tue, 05 May 2015

Thanks Jacob! I added it.

Diedre Tue, 12 May 2015

print has ben changed to print()
Thanks for the tutorials

Frank Tue, 12 May 2015

Thanks Diedre! It depends on the interpreter. print() for Python 3.0, print for the widely used Python 2.7

Wangyueyun Thu, 14 May 2015

very good !

Gajendra Garg Mon, 18 May 2015

invalid syntax in big = 358315791L # long, a very large number

Frank Mon, 18 May 2015

Thanks for your comment! The syntax is for Python 2.7; In Python 2.7, widely used, you have two separate types for int and long, and the above program will work. Python 3 is not fully backwards compatible and has only one int type.

If you are on Python 3 use this:


big = 358315791 # long, a very large number

You can check your Python version with:

python --version

Thanks for asking! More tutorials coming soon :-) Feel free to ask anything.

Rachel Mon, 01 Jun 2015

This tutorial is incredibly helpful! Thanks for taking the time to put it together.

Vivian Sun, 14 Jun 2015

f = 3.141529       # a floating real point
print (f)

output: 3.1415289999999998

Frank Sun, 14 Jun 2015

Hi vivian, this is a property of floating point numbers. It's not a bug in Python, it's caused by internal representation of floating point numbers. Floating point numbers only have 32 or 64 bits of precision, which is why the digits are cut off at some point. This is the case for all programming languages. If you want to store an exact amount use another variable storage such as Decimal.

#!/usr/bin/env python
from decimal import *

print Decimal('3.141529')

Floating points are often an approximation and should be used as such. Use Decimal or another datatype for exact numbers. A famous example of a floating point bug is that of the Ariane 5 spacecraft. Thanks! I hope you enjoy my site

Andyw Wed, 17 Jun 2015

I bought a Raspberry Pi, have been learning Unix/Linux/Debian/Raspbian, very interested in updating my programming skills. Recently retired after 35 years, last time I did programming on a regular basis was in the early '90s. Learned Assembly on DEC PDP 11/44 also Basic, Pascal, FORTRAN and C. Object Oriented programming has become a more difficult learning curve than I thought it would be. Your website has been very helpful.

Can you recommend a learning path, books or websites, tutorials, etc. that would take someone like myself with a background in programming and walk them through Object Oriented programming, Python, C++, Java, etc. I would like to learn how to write applications on the Android platform and possibly IOS.

Having a lot of fun getting my hands back into the technical side of things. I actually loaded a PDP emulator on an old DEC PC, then loaded RSX-11 on top. Was fun playing in that old operating systems 30+ years after I first used it to learn programming.

Prashant Thu, 18 Jun 2015

after writing the python code how to run

Frank Thu, 18 Jun 2015

Option 1: From an development program like PyCharm
Create a new project, write the code, press run (the green triangle).
You can get PyCharm from here: https://www.jetbrains.com/pycharm

Option 2: Run the code online
Use one of the online python versions, write the code and press run.

Option 3: From the command line. (probably most difficult method)
If you are on Windows:
1) Download and install Python https://www.python.org/downloads/windows/
2) Add Python to the system path so you can run it anywhere from the command line.
3) Run using

python program.py

3) You can make an executable from the program using py2exe.
On Mac OS, Linux: the same.

Frank Thu, 18 Jun 2015

Hi Andy, glad you like my site. If you want to learn more Python I recommend keeping an eye on this site, because I plan to add many more tutorials. I'll add some more on object oriented programming.

There are more than 5000 books on object oriented programming, it's hard to pick one but I'd pick one that has a lot of code examples. To learn object oriented programming is the easiest in Python, followed by Java and finally C++. Python has the highest level of abstraction of these languages, C++ is very low level which is great but not for learning OO.

Java is a very broad language and I think it has so many concepts that its best to grasp with a book or video course. Some popular books on Java are:
Thinking in Java (4th Edition) and Effective Java (2nd Edition)

Android runs Java by default using a virtual machine called Dalvik. This virtual machine translates Java code into machine level code. Android uses Linux as kernel and thus can run C++ code or Python code. Most of the Android apps are created in Java, which is quite a bit different from Python. For Android I recommend using Java or C++ because the userbase and codebase is very large, Google has a lot of documentation for Java and C++ on Android. There are several methods to run Python on Android such as ASE (Android Scripting Environment) or Kivy.

iOS has stricter rules and I'm not sure if it's legally allowed to run Python apps on their platform. Apps on iOS are made with Apples programming languages Objective C and Swift. I did find someone ported a Python interpreter to iOS: http://pythonforios.com/ but I haven't tested it.

Oh btw, it seems you can run Python on the Raspberry Pi :-)

Andyw Sat, 20 Jun 2015

Yes, the Raspberry Pi has Python. It runs Raspbian (Variant of Debian optimized for RPI). I'm doing all my current learning, writing test programs using Python on the RPI. It's an amazing little computer with incredible power for such low cost. I bought one for my grandson also, he's done some coding with Scratch. It's fun yet a difficult challenge getting back into software development after all these years. I will definitely come back to your site often seeking additional insight and code examples to continue my journey of discovery.

Avraham Mon, 22 Jun 2015

in python 3 seems raw_input was renamed to input

Goldy Tue, 23 Jun 2015

Why you said the int type is signed but only possitive? As I see the int type is natually signed and can be both negative and possitive.

Frank Tue, 23 Jun 2015

Hi, correct the int type in python is signed and can be both negative and positive, updated. thanks!

Manoj Wed, 24 Jun 2015

I just tried PyCharm but the print didn't work I had it to change to:

print (sum)

Also rap_input didn't work for me either...

Is there something wront in my setting in PyCharm?

Frank Wed, 24 Jun 2015

Hi Manoj, your settings are fine. Your PyCharm is using Python 3.x so you have to use print() and input() instead of raw_input().

Hichem Fri, 26 Jun 2015

hi everbody
I don't understand way when I write big = 358315791L in interpreter an error occured tell me "SyntaxError "

Frank Fri, 26 Jun 2015

Hi Hichem, which python version do you use?

Try this if you have Python 2.7:

#!/usr/bin/env python

big = 358315791
print big

If you have Python 3.x try this:

#!/usr/bin/env python

big = 358315791
Victor Fri, 03 Jul 2015

Maybe the tutorial should mention that in 3.x is is print(...) instead of print ..., -snip-

Frank Fri, 03 Jul 2015

Hi Victor, thanks for your comment! The tutorial has been updated.

Will Sat, 04 Jul 2015

I'm really enjoying these tutorials, thanks Frank

Frank Sat, 04 Jul 2015

Thanks Will!

Uwe Tue, 07 Jul 2015

The error is caused by the "L" after the number. I copied the tutorial code and got a Syntax error, after deleting "L" there was no error any more.

Aaiem Wed, 08 Jul 2015

Hi. I am beginner in programming. I have installed python 34. Every time i write your codes, that include any print command and i press enter key, it shows the output. I want to enter another line of code but output of first line appears. how can i fix that

Yuri Wed, 08 Jul 2015

Is error in first example of code a plan?

Frank Wed, 08 Jul 2015

Hi, to run a program you should start it with the filename. This means you save the code in a text editor and save to name.py
Then in the terminal you type:

python name.py

Frank Wed, 08 Jul 2015

Hi Yuri, no errors. Perhaps your python version does not deal with L or complex numbers. Try to test it with every type one by one :-)

Iman Sun, 12 Jul 2015

Hi Frank, in python 35-32 whenever I want to try any of the instructions above, it does not let me complete all of that . When I type the first line and press enter for the next lines, I see the command is done before finishing all of that . What's wrong with that ?

Frank Mon, 13 Jul 2015

Hi, you are using the python interpreter directly. Instead you can give python a file. Create a file with the code and save it as numbers.py
Then start with 'python numbers.py' or 'python c:\\myprograms\\numbers.py' (wherever you put it)

Ryan Thu, 16 Jul 2015

Was the value of f (set to 3.141529 in the code) supposed to be an approximation of pi? If so, then the final two digits are transposed. pi is approximately 3.1415926...

Frank Thu, 16 Jul 2015

Thanks Ryan! Updated

Mark Thu, 23 Jul 2015

Output answer still transposed :)

Frank Thu, 23 Jul 2015

Thanks Mark! Updated

Mckamala Tue, 04 Aug 2015

when i am giving the value for the long integer as L eg: big= 35869543L , i am getting the invalid syntax error in my window why i am getting this error

Frank Tue, 04 Aug 2015

Hi, this has something to do with the python version. Try if it works without the L

G.L. Fri, 14 Aug 2015

when i type the input, x y and sum, the error report said multiple statements found..why this occurs, how can i fix it.

G.L. Fri, 14 Aug 2015

when i type the user input x y, sum, print, the error report said that multiple statements found. how can i write the codes properly with multiple lines?

Frank Fri, 14 Aug 2015

Hi, you are using the python interpreter directly. To resolve the problem start python with a program filename (file.py). Make sure you use a text editor such as Pycharm, Atom, Idle etc.. save the program as file.py then execute. Every statement has to be on a new line. See also the videos

Tunidevone Thu, 20 Aug 2015


Ws Fri, 21 Aug 2015

This happened with me too. I'm using Python 3.4.3
Try doing n = 10**10
print (n)
That's how long numbers work

Saqib Ali Khan Tue, 25 Aug 2015

Hi David,
what is difference between "Python Shell" and "Python Interpreter" and what is their main job? What is Idle or Atom: Shell or interpreters?


[email protected] Tue, 25 Aug 2015

This is an awesome tutorial for beginning Python, this helped me understand it more in depth to start writing my own programs.

Frank Thu, 27 Aug 2015

Hi, seems David is not around. The Python interpreter is Python, it executes all your code.
To enter the Python shell, run Python.exe. You will see an output similar to this:

Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

You can type commands in the Python shell, but usually people use the Python interpreter with a python code file.
Atom and Idle are IDEs, desktop environment or 'specialized text editors for code'.

Usof Fri, 11 Sep 2015

x = int(raw_input("Enter x:"))
NameError: name 'raw_input' is not defined

can anybody help plz?

Frank Fri, 11 Sep 2015

Hi Usof, try with x = int(input("Enter x:"))

A.Mhatre Sat, 07 Nov 2015

which version of python you are using

Frank Sat, 07 Nov 2015

Python 3, but you can use either Python 2.7 or Python 3 for these tutorials, both versions are given.

Topssy Wed, 18 Nov 2015

num = input('Give me a number? ')
print('You said: ' + str(num)) pls explain the programme above.

Frank Fri, 20 Nov 2015

This program asks a number from the keyboard, this number is stored in the variable num. On the next line the variable num is shown on the screen.

Wenuka Wed, 25 Nov 2015

Do we have to use " #!/usr/bin/env python " always at the beginning?

Frank Thu, 26 Nov 2015

This depends on your computer. This is a line to indicate where Python is located. For example, on windows could be something like C:\Python\Python.exe

Sigitas Tue, 19 Apr 2016

Correcting integer section replace from sys.maxint to sys.maxsize python 3.

Rathinavelu Thiruvenkatam Sun, 29 May 2016

" floats have numbers behind comma" Sir, I do not see any comma;perhaps you mean dot.
Please correct me if I am wrong.
Thank you.

Frank Sun, 05 Jun 2016

Yes, behind the dot.