Python Fundamentals: Numbers, Strings, and Variables - Patrick Harris
post-template-default,single,single-post,postid-4688,single-format-standard,eltd-core-1.1.1,woocommerce-no-js,eltd-boxed,flow-ver-1.3.6,eltd-smooth-scroll,eltd-smooth-page-transitions,ajax,eltd-blog-installed,page-template-blog-standard,eltd-header-standard,eltd-sticky-header-on-scroll-up,eltd-default-mobile-header,eltd-sticky-up-mobile-header,eltd-menu-item-first-level-bg-color,eltd-dropdown-slide-from-bottom,eltd-dark-header,eltd-header-style-on-scroll,wpb-js-composer js-comp-ver-5.5.2,vc_responsive

Python Fundamentals: Numbers, Strings, and Variables

On the go? Have Polly read to you.

The Zen Of Python


What is the “Zen of Python?”


Let’s ask longtime Pythoneer Tim Peters, who condensed Python’s design principles into 19 aphorisms and hide them in an easter egg.


Open a Python shell and enter “import this.”




Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren’t special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one– and preferably only one –obvious way to do it.

Although that way may not be obvious at first unless you’re Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it’s a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea — let’s do more of those!\


These are the principles the language was designed with, here are the language’s simplest ingredients:




Python’s simplest built-in data types :

  • Booleans, (true or false values)
  • Integers (whole numbers i.e. 42 or 10342314)
  • Floats (numbers with decimal points or exponents, i.e. 3.14159 or 1.0e8 aka one times ten to the eight power, or 100000000.0)
  • Strings (sequences of text characters)




Programming languages allow you to define variables. Variables are named objects that refer to memory added in your computer which are defined for later use in your program.


Just think of them as names for values that you want to use in your program.


>>>a = 7
>>>my_name = 'patrick'

>>>import requests 
>>url =requests.get('http://pat.world')
<Response [200]>

>>>pie = 3.14


Math & Integers


Python has built-in support for integers (whole numbers ie 5 and 5,000,000 and floating point numbers (3.1416, 123.342, 1.87e5). You can calculate in Python with the math operators below:


+, -, *, /, //, %, **


Addition and subtraction work as you would expect:



#To specify a negative integer, add a "-" before the digits:

Division is a bit different:


The sign “/” carries out floating point division

While two backslashes -“//” – performs integer (truncating) division’


The // operation is referred to as “truncating” because it returns an integer and ‘truncates’ (to shorten or cut off) the remainder


ie 9/5 == 1.8

ie 9//5 == 1

Notice that there is no remainder in the second example, it has been truncated off.

Dividing by 0 kicks an error:


Traceback (most recent call last): 

File "<stdin>", line 1, in <module> 

ZeroDivisionError: division by zero


Traceback (most recent call last):

File "<stdin>", line 1, in <module> 

ZeroDivisionError: integer division or modulo by zero 

Run the code by pressing the “>_” sign and examine the variables below.



The math here looks trickier than it is.


When we see a “=” we immediately think equality.


In Python, this isn’t the case.


The expression on the right side of the “=” is calculated first, then assigned to the variable on the left side. Remember, one “=” in python means assignment, two “==” means equals to. ie:


a = 5
b = 5 
if a == b:
    print("It's equal!")
    print("The valus are different")


A word on operator precedence and bases in Python


Precedence is just the ‘order of operations’. That is, the sequence that math operation-types should be executed.


What would happen if we encountered


 2 + 3  * 4 = 14 
 2 + (3 * 4)

If we solved the addition first we’d have (2 + 3) *4 is 20. But, like most programming languages and standard math notation, multiplication has higher precedence than addition. So the multiplication is solved before the addition.


2 + (3 * 4)



Precedence in Python:

  1. Exponents (**) have the highest precedence.
  2. Positive, negative, bitwise (+, -, ~)
  3.  Multiplication and division (*,/,//,%)
  4. Addition and subtraction (+ -)




Integers are assumed to be decimal (base 10) unless you specify another base

A base is how many digits you can use until you need to “carry the one”(binary)

In Python you can express literal integers in 3 bases besides decimal:


0b or 0B for binary
0o or 0O for octal (base 8)
0x or 0X for hex (base 16)




One of the amendments from Python 2 to Python 3 was support for the Unicode standard. Because of this, Python can contain characters from any language in the world, plus any symbol you could think of.


Unlike other languages, strings in Python are immutable. This means they cannot be altered once they’re created.


Strings are designated with single or double quotes – ‘’ or “”.


In Python, you can


1) Convert data types with the function string  str()






2) escape with \


Python lets you escape characters within strings. By preceding a character with a backslash (/), you give it special meaning.

>>>splittin_strings = Hello ,/nThere,/n How are you doin?




How are you doing?


3) Combine with +


You can combine literal strings and string variables by using the + operator, ie:


>>>'Hello' + 'there'

'Hello there'

4) Get the length of a string with len()


The len() function counts the number of characters in a string





Post a Comment