Python
Python
PYTHON
FROM W3SCHOOL
WHAT IS PYTHON
Python is a programming language that is used to create software, websites, and analyze
data. It is easy to learn, efficient, and can run on many different platforms. Python is a general-
purpose language that is used in many fields, including data science, web development, and
software development.
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.
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.
0| P a g e
3/26/2025 1:02:10 PM
PYTHON SYNTAX COMPARED TO OTHER PROGRAMMING LANGUAGES
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.
PYTHON INDENTATION
indentation
content Correct example False example
Python uses indentation to if 5 > 2: if 5 > 2:
indicate a block of code. print("Five is print ("Five is
greater than two!") greater than two!")
Python will give you an
error if you skip the
indentation:
1|Page
3/26/2025 1:02:10 PM
COMMENTS
Comments start with a #, and Python will render the rest of the line as a comment:
Comments in Python:
content example
Comments starts with a #, and Python #This is a comment
will ignore them: print ("Hello, World!")
Comments can be placed at the end of a print ("Hello, World!") #This is a
line, and Python will ignore the rest of comment
the line:
A comment does not have to be text that #print ("Hello, World!")
explains the code, it can also be used to print ("Cheers, Mate!")
prevent Python from executing code:
MULTILINE COMMENTS
Python does not really have a syntax for multiline comments. To add a multiline
comment, you could insert a # for each line:
#This is a comment
#written in
#more than just one line
print ("Hello, World!")
Or, not quite as intended, you can use a multiline string. Since
Python will ignore string literals that are not assigned to a variable, you can add a
multiline string (triple quotes) in your code, and place your comment inside it:
"""
This is a comment
written in
more than just one line
"""
print ("Hello, World!")
2|Page
3/26/2025 1:02:10 PM
Casting
If you want to specify the data type of a variable, this can be done with casting.
Int 20
Float 20.5456645654
complex 1j
range range
dict
set
frozensat
bool
bytes
Byte array
3|Page
3/26/2025 1:02:10 PM
Memory view
none
You can get the data type of a variable with the type () function.
input output
X = 5 <CLASS 'INT'>
PRINT(TYPE(X))
Y = "JOHN" <CLASS 'STR'>
PRINT(TYPE(Y))
x = "John"
# is the same as
x = 'John'
CASE-SENSITIVE
a = 4
A = "Sally"
#A will not overwrite a
VARIABLE NAMES
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
4|Page
3/26/2025 1:02:10 PM
Rules for Python variables:
myVar
MYVAR
Myvar2
CAMEL CASE
myVariableName = "John"
PASCAL CASE
MyVariableName = "John"
5|Page
3/26/2025 1:02:10 PM
SNAKE CASE
my_variable_name = "John"
Note: Make sure the number of variables matches the number of values, or
else you will get an error.
And you can assign the same value to multiple variables in one line:
x = y = z = "Orange"
print(x)
6|Page
3/26/2025 1:02:10 PM
print(y)
print(z)
UNPACK A COLLECTION
If you have a collection of values in a list, tuple etc. Python allows you to extract the
values into variables. This is called unpacking.
OUTPUT VARIABLES
x = "Python is awesome"
print(x)
x = "Python"
y = "is"
z = "awesome"
print (x, y, z)
x = "Python "
y = "is "
z = "awesome"
print (x + y + z)
7|Page
3/26/2025 1:02:10 PM
x = 5
y = 10
print (x + y)
In the print () function, when you try to combine a string and a number with
the + operator, Python will give you an error:
x = 5
y = "John"
print (x + y)
The best way to output multiple variables in the print () function is to separate them
with commas, which even support different data types:
x = 5
y = "John"
print (x, y)
PYTHON NUMBERS
int
float
complex
Variables of numeric types are created when you assign a value to them:
8|Page
3/26/2025 1:02:10 PM
9|Page
3/26/2025 1:02:10 PM
10 | P a g e
3/26/2025 1:02:10 PM
11 | P a g e
3/26/2025 1:02:10 PM
12 | P a g e
3/26/2025 1:02:10 PM
13 | P a g e