Python From Scratch
Python From Scratch
SCRATCH
What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
➢ web development (server-side),
➢ software development,
➢ mathematics,
➢ system scripting.
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.
Why Python ?
➢ 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 Comments
➢ Comments can be used to explain Python code.
➢ Comments can be used to make the code more readable.
➢ Comments can be used to prevent execution when testing code.
Comments starts with a #, and Python will ignore them:
Example:
#This is a comment
print("Hello, World!")
To add a multiline comment, insert a # for each line:
Example:
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Add a multiline string (triple quotes) in your code, and place your comment inside it.
Example:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python Variables
Variables are containers for storing data values.
In Python, variables are created when you assign a value to it
Example:
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.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
print(x, y, z)
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
x = "John“
print(x)
# is the same as
y = 'John’
print(y)
Case-Sensitive
Variable names are case-sensitive.
a=4 a=4
print(a) a = "Sally"
print(A) print(a)
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows to extract the values
into variables. This is called unpacking.
x = "Python is awesome"
print(x)
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Use the + operator to output multiple variables
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
x=5
y = 10
print(x + y)
Try yourself….
x=5 x=5
y = "John" y = "John"
print(x + y) print(x, y)
Python - Variable Names
Variable Names
➢ A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume). Rules for Python variables: A variable name must start with a
letter or the underscore character
➢ A variable name cannot start with a number
➢ A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
➢ Variable names are case-sensitive (age, Age and AGE are three different variables)
➢ A variable name cannot be any of the Python keywords.
Legal variable names:
Illegal variable names:
myvar = "John"
2myvar = "John"
my_var = "John"
my-var = "John"
_my_var = "John"
my var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Variable names with more than one word can be difficult to read.
2. Pascal Case
Each word starts with a capital letter:
MyVariableName = "John"
3. Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"