🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

Numbers

Python has 3 numeric types; int, float and complex.

An int is an integer or whole number that can be positive or negative.

Integer Numbers

Sample Codeint1 = 5
int2 = 3
int3 = -9
print(int1, int2, int3) # Prints 5 3 -9

A float of floating point number can be positive or negative, containing one or more decimals. Scientific numbers with the letter “e” can be used to indicate the power of 10.

Float Numbers

Sample Codefloat1 = 5.0
float2 = 3.0
float3 = -9.0
float4 = -27.1e10
print(float1, float2, float3, float4) # Prints 5.0 3.0 -9.0

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

Complex Numbers

Sample Codecomplex1 = 3+5j
complex2 = 5j
complex3 = -9j
print(complex1, complex2, complex3) # Prints (3+5j) 5j (-00-09j)
# Type Some Code

Basic Data Types

The str type is a sequence of characters inside double or single quotes.

The int type is a non-decimal whole number and can be either positive or negative.

The float type is for floating point numbers also called double with a decimal point.

The bool type can only be True or False.

Arrays are created using list and tuple or set types to store multiple values in a single variable.

NoneType has no assigned value.

You can get the data type of any object by using the built-in type function.

Data Type

Sample Codemy_string = “Hello world!”
print(my_String) # Prints Hello world!
print(type(my_string)) /# Prints str

Casting allows the variable type to be changed for the int, float and str data types.

Casting Integers

Sample Codeint1 = int(5) # Will be 5
int2 = int(3.0) # Will be 3
int3 = int(“9”) # Will be 9

Casting Floats

Sample Codefloat1 = float(5) # Will be 5.0
float2 = int(3.0) # Will be 3.0
float3 = int(“9.0”) # Will be 9.0

Casting Strings

Sample Codestr1 = str(“s5”) # Will be ‘s5’
str2 = str(“3”) # Will be ‘3’
str3 = str(“9.0”) # Will be ‘9.0’

# Type Some Code

Chapter 2: Data Types

A data type is a grouping of data values. Variables are created to store a data type. In Python, the assigning of a value will indicate the data type. PHP does not require a specific format in order to display the value.

Table : Data Types

TypeDescriptionExample
strStores a sequence of charactersname = “Jake”
intStores a non-decimal numberage = 24
floatStores a decimal point numberamount = 10.25
complexStores a complex numbercomplex_num = 2 + 4j
boolStores either True or Falseis_available = True;
listStores collection of valuesnames = [“Roy”, “Joe”, “Ben”]
tupleStores collection of valuesnames = (“Roy”, “Joe”, “Ben”)
rangeStores sequence of numbersnums = range(-1,2)
dictAssociative array or dictionaryperson = {“name”: “Jake”, “age”: 24}
setUnordered mutable set with no duplicatesno_dups = set(((“Roy”, “Joe”, “Ben”))
frozensetUnordered immutable set with no duplicatesno_dups = frozenset(((“Roy”, “Joe”, “Ben”))
bytesStores immutable sequence of bytesmy_byte = b”Some Text”
bytearrayStores mutable sequence of bytesmy_byte = bytearray([11, 22, 33])
NoneTypeStores only NoneType valuename = None
typeReturns type of objectresult = type(name)

Variables

Variables store information such as values or other variables.

Python variables do not need to be declared with any type and the type can be changed after been set.

Variables cannot start with a number because they must start with an underscore or letter. The second character can contain only alphanumeric characters or underscores.

Variable names are case-sensitive.

A variable name cannot be any of the Python keywords.

Variable Names

Sample Codename = ‘John’ #name is type str
age = 18 # age is type int
height = 5.3 # height is type float
myVarName = ‘Johnny’ # Camel Case is where each word, except the first, starts with a capital letter
MyVarName = ‘Johnny’ # Pascal Case is where each word starts with a capital letter
my_var_name = ‘Johnny’ # Snake Case is where each word is separated by an underscore
car = fruit = color = ‘red’ # Assign the same value to multiple variables on one line

Output Variables

Sample Code
print(name) # Outputs a single variable
print(name, age, height) # Outputs multiple variables, separated by a comma
print(name + myVarName + car) # Outputs multiple non numeric variables, separated by a plus symbol

# Type Some Code

Comments

Single Line Comments

Hash symbol # will mark all text until a newline as a comment.

Sample Code# This is a single line comment

Multiline Comments

Python does not have a syntax for multiline comments. One way to achieve a multiline comment could be starting each line with a hash symbol.

Method 1 For Multiline Comments

Sample Code# This is a comment
# that goes on
# multiple lines

The other way to achieve a multiline comment is to use a multiline string because Python ignores string literals not assigned to a variable. A multiline string has tripe quotes.

Method 2 For Multiline Comments

Sample Code“””
This is a comment
that goes on
multiple lines
“”””

# Type Some Code

Syntax

Python has a simple syntax and grammar which embraces one way of achieving a task. It does not use curly brackets to delimit blocks, but semicolons after statements are allowed.

Python uses white-space indentation to delimit blocks. An increase in indentation comes after certain statements. A decrease in indentation signifies the end of the current block. The suggested indentation size is 4 spaces.

Hello World Example

Sample Codeprint(‘Hello world’) # Prints Hello world
# Type Some Code

History

Python is a popular general-purpose scripting language that is maintained by the Python Software Foundation and is widely used for machine learning. It is released under the open source Python Software Foundation License and its official website is https://www.python.org.

Guido van Rossum first released Python as version 0.9.0 in 1991. A new version is released annually with support for 2 years and security updates for another 3 years.

Python can run on the command line or on a web server by an interpreter. Web frameworks exist to develop complex applications. Client-side Ajax-based applications can also be created to run Python using JavaScript.

Python as a scripting language, is commonly used in artificial intelligence projects and embedded is software products such as FreeCAD, Blender, Gimp, Inkscape, Scribus including notation programs.

Table : Recommended Python Versions

VersionReleasedSupport EndSecurity End
3.112022-10-242024-04-012027-10-31
3.122023-10-022025-04-022028-10-31

Chapter 1: Getting started with Python

In this chapter, you will learn about the brief history of Python, its syntax, comments and string formatting. You can use your favorite text editor, IDE or use the built-in IDE. The built-in IDE can also be used to compile your code.

If you use your own text editor or IDE, you will need to also install Python in order for your code to be interpreted. The built-in IDE has syntax highlighting using different colors for the keywords, comments, control-flow statements, variables and other elements.

To use the built-in IDE simple type the code in the IDE and click the compile button to display the results in the output area. Otherwise follow the requirements for the text editor and Python.

Requirements:

  1. Optional text editor for programming recommended
  2. Optional Python installed and configured

About

Edward Ojambo has been programming in various languages since 1985. He works as a freelance programmer creating custom databases, web, desktop and mobile applications. Private one-on-one tutorials are offered for computer programming, website development, custom applications, Linux, email servers, web servers and search engine optimization.

All content was created for educational purposes and is not affiliated with official Python. The content was extracted from examples that Edward Ojambo uses during work-related activities.

The content is released under Creative Commons BY-SA 4.0, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content present in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to OjamboShop.com.

Learning Python

PROGRAMMING eBook LEARNING PYTHON OjamboShop.com eBook created by
Edward Ojambo