ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Numbers In Python - Part 2

Updated on May 31, 2017

This is Part 2 of the Python Coding Series.

  • Part 1: Introduction to Python


Numbers work incredibly different in Python than other coding languages. One of the first foundations we need to establish with Python coding is learning how to store information. Computers are really smart at handling information. A program uses variables to make this work.

An integer, float, and a long variable walk into a bar...

There are three main variable types associated with numerical values.

  • Integer (int): An integer is a number that does not contain a fraction or decimal. (ex: 1, -6, 20 , 50)
  • Float: Floats are known as decimal numbers (3.14, -5.69,25.25)
  • Long: A long variable is able to store a large numerical value.

Each variable is crucial to understanding what they are and what they can be. However, in Python, there is no need to define what the variable type is because Python will identify them for you. In Java, we would normally define an integer with either int, float, or long before we state the name of the variable. In Python, we just assign the variable to the number as seen in Example 1.

Example 1

a = 5
//Output is the number 5

If you want to return the type of a variable all you would do is use the type(var) function. This function will return what the type of that variable is as seen in Example 2.

Example 2

//returns <type 'int'>

If you ever run into problems associated with numbers, then sometimes it had to do with how you are handling numbers. Using the type function is key to identifying whether the compiler did not like how you handled the numbers.

How to Name a Variable

Variable names take up memory so giving a long title for a variable is not ideal. There are rules associated with naming variables that every coder should know.

  1. Names cannot begin with a number. The compiler will not like this.
  2. Names cannot be too long due to memory spent use.
  3. Names cannot contain any special characters like $,%,^,&,*,!,(,@,# though the underscore (_) is great to use with variables that are given more than one word.
  4. If a variable is a constant and does not change in the program, assign that variable to all caps like TAX or INTEREST.
  5. Make sure that your variable name makes sense. Do not assign the word 'apple' for a variable that should be labeled 'orange'.
  6. Rather than writing numberofcars consider writing it as number_of_cars
  7. Make all variables lowercase
  8. Avoid using the letter 'l' or 'o' if representing a variable as a single letter. This can be confused as the number 1 or 0.

Now that we have the rules in place, let's begin doing some math in Python!

Math in Python

Math in Python works just like normal math with the use of operators. There are about 9 operators that can be used.

1 + 1 returns 2
2 - 1 returns 1
5 * 2 returns 10
6 / 2 returns 3
Floor Division
13 // 2 returns 6
13 % 2 returns 1
-a returns -5 if a =5
Absolute Value
abs( )
abs(-5) returns 5
2**6 returns 64

Addition, subtraction, multiplication, and division work the same as regular math. Floor division is when you divide two numbers and round to the floor. Modulo will return the remainder of dividing the two numbers. Modulo is an important operator because it can be used to determine if a number is odd or even. Negation negates a variable. Absolute value returns the absolute value of the variable. The exponent operator in Python is strange as it features two asterisks instead of the usual '^' symbol. This will assign the 2nd number as the exponent value of the first.

Key Points

There are some few important points to understand when working with numbers. First of all combining different types of numbers when doing math will often change the variable's type. For example, the type of 1 +1 is an integer, but the type returned by the summation of 1.0+1 is a float, not an integer because of the addition of a float. Remember this when working with different data types. When dealing with floats, it is best to go ahead and represent any integers as floats.

Also, remember that you cannot violate math in Python. If you divide by zero, you will get a ZeroDivisionError. If you ever get this error, that means you tried to divide zero somewhere in the program.

Python follows Order of Operations

The Order of Operations is a set of guidlines to solving complex equations that feature more than one type of operator. The PEMDAS acronym is used when solving order of operations problems. When solving a complex problem in python it will follow this order from top to bottom.

  • Parenthesis
  • Exponents
  • Multiplication
  • Division
  • Addition
  • Subtraction

Here are some examples:

5 + 6 * 2
5 + 6 ** 4
(5 + 6) * (2 + 5 * 2)
5 + (4 * (4 + 2))


In this part of the Python Coding series, we learned about the different numerical variables in Python. We also learned how to name a variable and the rules associated with doing so. In the next part of this series, we will learn about Logic in Programming which involved conditional statements.

Quiz Yourself!

view quiz statistics


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)