Tag Archives: programming

Learning Python – Lesson 5: More on Numbers and Math

Python Programming

It’s time to dig deeper into our work with numbers (including math) and strings.

What is Your Type?

First of all, it might be handy (especially as you learning), to have Python report the type of a variable or a literal value. For example:

C:\Users\terry>py
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(199.9)
<class 'float'>
>>> type("Howdy!")
<class 'str'>
>>> type(10043)
<class 'int'>
>>> a = "This is cool!"
>>> type(a)
<class 'str'>
>>>

More on Division

Division in Python comes in two variations:

/ carries out floating point division

// caries out integer (truncating) division

For example:

>>> 15 / 4
3.75
>>> 15 // 4
3

Math Precedence

Many of the math precedence rules would make sense to us (those that took some math in school!) For example, multiplication wins over addition. But fortunately, Python supports the use of ( ) to indicate precedence. This keeps us from having to worry about default behaviors. Here is an example:

Continue reading Learning Python – Lesson 5: More on Numbers and Math

Learning Python – Lesson 4: More on Variables

Python Variables

Here we will dig deeper into the important concept of variables in Python. In this lesson, I will test my code and provide the output using Python in what is called interactive mode. Here I am not building a script (script mode), but I am typing my commands into my terminal and getting results back immediately. To enter interactive mode, I just launch Python and I do not specify a script to execute:

C:\Users\terry\OneDrive\Blog\Python>python
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

How Variables Work in Python

Here are some important things to consider:

Continue reading Learning Python – Lesson 4: More on Variables