Python Tutorials

Overview

There are three numeric types in Python: int, float, and complex. Variables of numeric types are created when assigned a value.

x = 8    #int
y = 1.5  #float
z = 1j   #complex

Int

Int stands for integer, and is a whole number, positive or negative, without decimals, of unlimited length.

x = 8
y = 155888112
z = -416

Float

Float stands for “floating point number”, and is a number, positive or negative, containing one or more decimals.

x = 4.10
y = 8.0
z = -10.188

Float can also be scientific numbers with an “e” to indicate the power of 10.

x = 18e3
y = 12E4
z = -68.5e100

Complex

Complex numbers are written with a “j” as the imaginary part.

x = 3+8j
y = 8j
z = -2j

Type Conversion

You can convert from one type to another with the int()float(), and complex() methods.

x = 6    #int
y = 2.8  #float
z = 4j   #complex

#convert from int to float
a = float(x)

#convert from float to int
b = int(y)

#convert from int to complex
c = complex(x)

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.