Introduction to Python Programming Language for Industrial
Engineering 4th Students
CHAPTER TWO
PYTHON BASICS
Python Syntax
Python syntax can be executed by writing directly in the Command Line:
Or by creating a python file on the server, using the .py file extension, and running it in the
Command Line:
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
Python uses indentation to indicate a block of code.
Python will give you an error if you skip the indentation:
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
The number of spaces is up to you as a programmer, the most common use is four, but it has to be
at least one.
You have to use the same number of spaces in the same block of code, otherwise Python will give
you an error:
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.
Creating a Comment
Comments starts with a #, and Python will ignore them:
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
A comment does not have to be text that explains the code, it can also be used to 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:
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:
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
Python Variables
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.
❖ Variables do not need to be declared with any particular type, and can even change type
after they have been set.
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
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 - Variable Names
Variable Names
❖ A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
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.
Example:
Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Multi Words Variable Names
❖ Variable names with more than one word can be difficult to read.
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
There are several techniques you can use to make them more readable:
Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "John"
Pascal Case
Each word starts with a capital letter:
MyVariableName = "John"
Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
Python Variables - Assign Multiple Values
Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line:
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Note: Make sure the number of variables matches the number of values, or else you will get an
error.
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
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.
Example
Unpack a list:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Python - Output Variables
Output Variables
❖ The Python print() function is often used to output variables.
Example
x = "Python is awesome"
print(x)
❖ In the print() function, you output multiple variables, separated by a comma:
Example
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
❖ You can also use the + operator to output multiple variables:
Example
x = "Python "
y = "is "
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
z = "awesome"
print(x + y + z)
Notice the space character after "Python " and "is ", without them the result would be
"Pythonisawesome".
Python Data Types
Built-in 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 Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
Getting the Data Type
❖ You can get the data type of any object by using the type() function:
Example
Print the data type of the variable x:
x=5
print(type(x))
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
Setting the Data Type
❖ In Python, the data type is set when you assign a value to a variable:
Example 1:
x = "Hello World"
#display x: Output
print(x) Hello World
#display the data type of x: <class ‘str’>
print(type(x))
Example 2:
x = 20
#display x: Output
print(x) 20
#display the data type of x: <class ‘int’>
print(type(x))
Example 3:
x = 20.5
#display x: Output
print(x) 20.5
#display the data type of x: <class ‘float’>
print(type(x))
Example 4:
x = 1j
#display x: Output
print(x) 1j
#display the data type of x: <class ‘complex’>
print(type(x))
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
Example 5:
x = ["apple", "banana", "cherry"]
#display x: Output
print(x) ["apple", "banana", "cherry"]
#display the data type of x: <class ‘list’>
print(type(x))
Example 6:
x = ("apple", "banana", "cherry")
#display x: Output
print(x) ("apple", "banana", "cherry")
#display the data type of x: <class ‘tuple’>
print(type(x))
Example 7:
x = range(6)
#display x: Output
print(x) range(0,6)
#display the data type of x: <class ‘range’>
print(type(x))
Example 8:
x = {"name" : "John", "age" : 36}
#display x: Output
print(x) {"name" : "John", "age" : 36}
#display the data type of x: <class ‘dict’>
print(type(x))
Example 9:
x = {"apple", "banana", "cherry"}
#display x: Output
print(x) {"apple", "banana", "cherry"}
#display the data type of x: <class ‘set’>
print(type(x))
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
Python Operators
❖ Operators are used to perform operations on variables and values.
❖ In the example below, we use the + operator to add together two values:
Python Arithmetic Operators
❖ Arithmetic operators are used with numeric values to perform common mathematical
operations:
Addition
x=5
y=3 Output
print(x + y) 8
Subtraction
x=5
y=3 Output
print(x - y) 2
Multiplication
x=5 Output
y=3 15
print(x * y)
Division
x = 12
y=3 Output
print(x / y) 4
Modulus
x=5 Output
y=2 1
print(x % y)
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024
Introduction to Python Programming Language for Industrial
Engineering 4th Students
Exponentiation
x=2
y=5 Output
print(x ** y) #same as 2*2*2*2*2 32
Operator Precedence
❖ Operator precedence describes the order in which operations are performed.
Example
❖ Parentheses has the highest precedence, meaning that expressions inside parentheses must
be evaluated first:
print((6 + 3) - (6 + 3))
❖ The precedence order is described in the table below, starting with the highest precedence
at the top:
❖ If two operators have the same precedence, the expression is evaluated from left to right.
Example
Addition + and subtraction - has the same precedence, and therefore we evaluate the expression
from left to right:
print(5 + 4 - 7 + 3)
Compiled By: Mr. Debelo Negeri ([Link].) May, 2024