Python variables


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 Crash Course: Introduction to Python Programming

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
 
print(x)
print(y)
print(z)

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

#!/usr/bin/env python
 
x = 3
y = 8
 
sum = x + y
 
print(sum)

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
print(sum)

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
print(sum)
Back
Next

59 thoughts on “Python variables

  1. Usof - September 11, 2015

    x = int(raw_input(“Enter x:”))
    NameError: name ‘raw_input’ is not defined

    can anybody help plz?

    1. Frank - September 11, 2015

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

    2. A.Mhatre - November 7, 2015

      which version of python you are using

      1. Frank - November 7, 2015

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

  2. [email protected] - August 25, 2015

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

  3. Saqib Ali Khan - August 25, 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?

    Thanks

    1. Frank - August 27, 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’.

  4. Tunidevone - August 20, 2015

    Great!..thanks..!

  5. G.L. - August 14, 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?

    1. Frank - August 14, 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

  6. G.L. - August 14, 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.

    1. Frank - August 14, 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

Leave a Reply