Introduction to
Python Programming
Object Oriented Programming (CpE 2)
Engr. CJASantos
Contents
1. Introduction to Python
2. The print() function, output formatting
3. Comments
4. Indentation
5. Variables
6. Data Types
7. Casting
8. The input() function
9. Operations and Expressions
Introduction to Python
What is Python?
• Python is an interpreted, object-oriented, high-level
programming language.
• It was created by Guido van Rossum, and released in 1991.
Introduction to Python
Why use Python?
• Easy to understand
• It can be used for creating:
o Web Applications
o Data Science and Data Visualization
o AI and Machine Learning
o Script for Vulnerability Testing
Introduction to Python
Requirements
• Python Interpreter
• Text Editor:
o Sublime
o VS Code
• IDE Integrated Development Environment (Optional):
o PyCharm
o Anaconda
Introduction to Python
How to execute Python code
• IDLE (IDLE is Python’s Integrated Development and Learning
Environment)
• Execute via Command Line (Windows)
• Creating a Python file
Introduction to Python
Try to execute Python Syntax in Command Line:
Output
print() function Example:
• You can use single or
double quotes to print a
data or element in
Python.
Comments
Comments Examples:
• Comments starts with a #, and
Python will ignore them.
• Comments can be placed at the end
of the 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:
Comments
Multi-line Comments
Example:
Indentation
Indentation Example:
Syntax Error
• 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 Correct
in Python is very important.
• Python uses indentation to indicate
a block of code.
Variables
Variables Example:
• Variables are containers for
storing data values.
• A variable is created the
moment you first assign a
value to it.
Variables
Naming Variables
Legal variable names: Illegal variable names:
Variables
Multi Words Variables Names
• Camel Case – Each word, except the first, starts with a capital letter.
Example: myVariableName = “Programming”
• Pascal Case – Each word starts with a capital letter.
Example: MyVariableName = “Programming”
• Snake Case – Each word is separated by an underscore character.
Example: my_variable_name = “Programming”
Variables
Many Values to Multiple Variables
• Python allows you to assign values to multiple variables in
one line.
Example:
Variables
One Value to Multiple Variables
• You can assign the same value to multiple variables in one
line.
Example:
Variables
Output Variables for Strings
• In the print() function, you output multiple variables, separated by
comma.
• You can also use the + operator to output multiple variables.
Example:
Variables
Output Variables for Strings and Numbers
• For numbers, the + character works as a mathematical operator.
• In the print() function, when you try to combine a string and a number with
the + operator, Python will give you an error.
• Best way to output multiple variables in print() function is to separate then
with commas, which even support different data types.
Example:
Data Types
Most Commonly Used Data Types
Type of data stored in a variable:
• String = str
• Integer = int
• Float = float
• Boolean = bool
Casting
Casting Example:
• If you want to specify
the data type of a
variable, this can be
done with casting.
Casting
Casting Examples:
• Converting one data
type into another is
known as casting.
Input
input() function Example:
• Accepts user’s input.
Operations & Expressions
Python Arithmetic
Operators
• Arithmetic operators
are used with numeric
values to perform
common mathematical
operations:
Operations & Expressions
Python Assignment
Operators
• Assignment operators
are used to assign
values to variables:
Operations & Expressions
Python Comparison
Operators
• Comparison
operators are used
to compare two
values:
Operations & Expressions
Example:
Seatwork
1. Print your Full Name. 1pt
2. Write the correct syntax for the variable name myProject with a
value of Python Programming. 1pt
3. Let the user enter his/her First Name, Middle Name and Last
Name. Afterwards, print the user’s Full Name in one line in this
order (Last Name, First Name, Middle Name). Example: Santos,
Crystaleene Jade Aragones 3pts
4. Let the user input two numbers then display their sum,
difference, product and quotient. 5pts