• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Using math in python

Using math in python

Author: PFB Staff Writer
Last Updated: May 25, 2020

Math in Python

The Python distribution includes the Python interpreter, a very simple development
environment, called IDLE, libraries, tools, and documentation.

Python is preinstalled on many (if not all) Linux and Mac systems,
but it may be an old version.

Calculator

To begin using the Python interpreter as a calculator, simple type python in the
shell.

>>> 2 + 2
4

>>> 4 * 2
8

>>> 10 / 2
5

>>> 10 - 2
8

Counting with variables

Put in some values into variables to count the area of a rectangle

>>> length = 2.20
>>> width = 1.10
>>> area = length * width
>>> area
2.4200000000000004

Counter

Counters are useful in programming to increase or decrease a value everytime it
runs.

>>> i = 0
>>> i = i + 1
>>> i
1
>>> i = 1 + 2
>>> i
3

Counting with a While Loop

Here is an example showing when it’s useful to use counters

>>> i = 0
>>> while i < 5:
...     print i
...     i =  i + 1
... 
0
1
2
3
4

The program counts from 0 to 4. Between the word while and the colon, there is an
expression that at first is True but then becomes False.

As long as the expression is True, the following code will run.

The code that needs to be run has to be indented.

The last statement is a counter that adds 1 to the value for everytime the loop
runs.

Multiplication table

Making a multiplication table in Python is simple.

table = 8
start = 1
max = 10
print "-" * 20
print "The table of 8"
print "-" * 20
i = start
while i <= max:
    result = i * table
    print i, " * ", table, " =" , result
    i = i + 1
print "-" * 20
print "Done counting..."
print "-" * 20

>>Output:

——————–
The table of 8
——————–
1 * 8 = 8
2 * 8 = 16
3 * 8 = 24
4 * 8 = 32
5 * 8 = 40
6 * 8 = 48
7 * 8 = 56
8 * 8 = 64
9 * 8 = 72
10 * 8 = 80
——————–
Done counting…
——————–

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us