Introduction to Python

What is Python?

Python is a programming language used to create programs that tell computers, step-by-step, how to solve problems. Programs are behind all of the computer applications you use every day. Software engineers at Google write programs that power the applications you are familiar with such as Search, Gmail, and Maps. As you will soon learn, you too can write programs to solve interesting problems.

Mathematical Notation in Python 

Python uses several symbols for mathematical operations that vary from what we usually see in math class. Test the examples below while looking for patterns that indicate the meaning of each symbol. 

OperatorSymbolExample 1Example 2Explanation
Exponent   **>>> 7**249>>> 2**416Repeated multiplication
Modulo%>>> 10 % 31>>> 24 % 54Remainder after division
Division/>>> 19 / 53>>> 21 / 4.05.25Divides, rounds down for integers

Parentheses in Python 

Parentheses can change the result of an arithmetic expression. In the Python interpreter, enter the following expressions to understand how parentheses affect the order of operations.


>>> 6 + 10 / 2
>>>(6 + 10) / 2
>>> 2 + 3 * 5
>>>(2 + 3) * 5
>>> 6 +(8 / 4) – 2 * 3
>>>(6 + (8 / 4) – 2) * 3


  • Be sure to include all the parentheses you need to perform the correct order of operations.
  • Be sure to close any parentheses that you open.

Testing for Equality in Python 

Python has three different symbols to represent different types of equality 

Example 1Example 2Example 3
>>> x = 7
>>> x * 4
>>> x**2
>>> x
>>> x = x * 4
>>> x = 10
>>> y = 2
>>> x == y   #Equal toFalse
>>> x != y   #Not equal toTrue
>>> y = 10
>>> x == yTrue
>>> x = 10
>>> y = 2
>>> x < y #Less than
>>> x <= 10 #Less than or equal to
>>> x >=11

Programs in Python 

  1. To use the Interactive Mode in Python, type a command at the >>> prompt and press ‘Enter’.’
    Example:  >>> print ‘Hello World’

  Hello World

  1. To to write multi-line programs in Python using the Script Mode:
    • To write code: From the IDLE Shell: Ctrl-N/Command-N or FileNew Window
    • To run code: Press F5 or choose RunRun Module → Save with .py extension.


  • Quotation marks and commas are NOT optional in Python.  If Python gives you an error message, double check to make sure you included all quotation marks and commas!
  • Comments start with a # and are ignored by Python. It is used to help others read their code.
  • Variables in Python with more than one word are separated by an underscore ex: variable_name. 
  • To create an underscore, hold Shift and press the key to the right of the number 0.

Activity:Open a Python editor (2a, above) and type or copy the code below. Then run the code (2b).

#Asks the user to type in an answer and press enter.
temp_in_f = input('What is the temperature in Fahrenheit: ')

temp_in_c = (temp_in_f - 32) * 5 / 9.0

#Displays the information
print temp_in_f, 'Fahrenheit is', temp_in_c, 'in Celsius.' 

Sample Output:

What is the temperature in Fahrenheit: 80

80 Fahrenheit is 26.6666666667 in Celsius.

Conditional Logic in Python  

Python uses conditional logic to ‘branch out’ and handle all possible scenarios to solve a problem. 


  • You must include a colon (:) at the end of each if, elif, and else statement, which tells Python to automatically indent the following line.


secret_number = 7
guess = input('Guess what number I am thinking of: ')

if guess == secret_number:
    print 'You got it!'
elif guess < secret_number:
    print 'Too low.'
    print 'Too high.'

Programming with Loops in Python 

Python uses a ‘loop’ to complete a repetitive task very quickly.  A while loop performs a specified task over and over while a specified condition is true.  Run the ‘blastoff’ program below to see the loop in action.


number = input('Enter a number between 1 and 50: ')

while number >= 0:
    print number
    number = number - 1
print 'Blastoff!'

We can have Python count the total number of multiples of 3 between zero and one hundred by adding a variable that we chose to call counter to our program. 


x = 3
counter = 0

while x < 100:
    print x
    counter = counter + 1
    x = x + 3
print 'There are', counter, 'multiples of 3 between zero and one hundred.'


  • We set counter equal to zero at the beginning of the program.  We must ‘initialize’ all variables before using them in a loop. This tells Python that it will need to save some space for the variable before it runs through the loop that follows. 

Python Outside of Math Class

The same programming skills that you use when solving math problems can create games as well.  

The previous ‘guess the number’ program has two drawbacks. 1) It needs to be run over and over, and 2) the programmer knows the secret number and cannot play. Python has a built-in function called random that will pick a random number between any two values that you want. 


import random

guess = input('Guess the number between 1 and 25: ')
secret_number = random.randint(1, 25)
while guess != secret_number:
  guess = input('Try again: ')

print ('You got it!  My number is', secret_number)

Add the conditional logic from above to help the user out.


import random
guess = input('Guess the number between 1 and 25: ')
secret_number = random.randint(1, 25)
while guess != secret_number:
  if guess < secret_number:
    guess = input('Too low, try again: ')
    guess = input('Too high, try again: ')

print ('You got it!  My number is', secret_number)


In this reference document we covered several key programming topics that are directly applicable to math curriculum and computational thinking. These include:

  • Parentheses affect the order in which Python performs a calculation
  • Python stores one value in each variable
  • Python uses several symbols rarely found outside of computer science to evaluate mathematical expressions, including: exponents, modulo, multiplication, division, and equality ==, >=, <
  • We can use the Python Editor mode to write our own programs
  • We can use conditional logic to account for several possible cases of the same problem
  • We can use ‘while loops’ to perform a repetitive calculation quickly and accurately
Previous Article

UPSC CSE Prelims: GS Paper-II CSAT RC Questions

Next Article

How to Teach Yourself to Code?