Introduction

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).

Python has a simple syntax similar to the English language.

Python has syntax that allows developers to write programs with fewer lines than some other programming languages.

Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.

Python can be treated in a procedural way, an object-oriented way or a functional way.

Python was designed for readability, and has some similarities to the English language with influence from mathematics.

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.

What can Python do
  • Python can be used on a server to create web applications.
  • Python can be used alongside software to create workflows.
  • Python can connect to database systems. It can also read and modify files.
  • Python can be used to handle big data and perform complex mathematics
  • Python can be used for rapid prototyping, or for production-ready software development.
  • It is used for:

  • Web development(server-side)
  • Software Development
  • Mathematics
  • System Scripting
Executing Syntax
Python syntax can be executed by writing directly in the Command:
>>> print("Hello,World")
Hello, World

Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:
C:\Users\Your Name>python myfile.py

Python Variables

Variables are containers for storing data values.

Creating Variables

Python has no command for declaring a variable

A variable is created the moment you first assign a value to it.

x = 5
y = "John"
print(x)
print(y)

Variables do not need to be declared with any particular type, and can even change type after they have been set.

Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)

Casting

If you want to specify the data type of a variable, this can be done with casting.

Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0

Get the Type

You can get the data type of a variable with the type() function.

Example
x = 5
y = "John"
print(type(x))
print(type(y))

Single or Double Quotes?

String variables can be declared either by using single or double quotes:

Example
x = "John"
# is the same as
x = 'John'

Case-Sensitive

Variable names are case-sensitive

Example
This will create two variables:
a = 4
A = "Sally"
#A will not overwrite a
Python Data Types

Python Data Types

In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

  • Text type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Mapping Types: dict
  • Set Types: set, frozenset
  • Boolean Types: bool
  • Binary Types: bytes, bytearray, memoryview
Python if Else

Python Conditions and if statements

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
Python Boolean

Booleans represent one of two values: True or False.

Boolean Values

In programming you often need to know if an expression is True or False.

You can evaluate any expression in Python, and get one of two answers, True or False.

When you compare two values, the expression is evaluated and Python returns the Boolean answer:

print(10 > 9)
print(10 == 9)
print(10 < 9)

Example:

Print a message based on whether the condition is True or False:

a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Python While Loops

Python Loops

Python has two primitive loop commands:

The while Loop

With the while loop we can execute a set of statements as long as a condition is true.

Example
Print i as long as i is less than 6:
i = 1
while i < 6:
print(i)
i += 1

The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.


The break Statement

With the break statement we can stop the loop even if the while condition is true:

Example
Exit the loop when i is 3:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1

The continue Statement

With the continue statement we can stop the current iteration, and continue with the text:

Example
Continue to the next iteration if i 3:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)

The else Statement

With the else statement we can run a block of code once when the condition no longer is true:

Example
Print a message once the condition is false:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Additional

For more information: Click Here