0% found this document useful (0 votes)
2 views57 pages

Week2-Programming in Python

This document provides an overview of programming in Python, covering fundamentals such as programming structures, algorithms, and data types. It highlights the differences between compilation and interpretation, as well as the distinctions between Python and Java. Additionally, it discusses various data structures like lists, tuples, and dictionaries, along with their built-in functions.

Uploaded by

Lizbeth Tabuena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views57 pages

Week2-Programming in Python

This document provides an overview of programming in Python, covering fundamentals such as programming structures, algorithms, and data types. It highlights the differences between compilation and interpretation, as well as the distinctions between Python and Java. Additionally, it discusses various data structures like lists, tuples, and dictionaries, along with their built-in functions.

Uploaded by

Lizbeth Tabuena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

DASTRUC

Programming in Python
Learning Outcomes
At the end of this module, you will:
➢ Review the fundamentals of computer programming
➢ Review the Three Basic Programming Structures
➢ Explain the difference between compilation and
interpretation
➢ Identify essential differences between Python and Java
➢ Be able to create and execute a Python program
Fundamental of Computer Programming

Computer Programming
- Writing instruction or code that a computer can execute to
perform specific tasks.

Data Structure
Fundamental of Computer Programming

Algorithm
- A step-by-step to solve a problem or performing a
computation.
Flowchart
- Visual tools that understand and represent processes using
symbols such as arrows, rectangles and diamonds show
steps and decision clearly.
Data Structure
Fundamental of Computer Programming

Programming Languages
- Languages are used to write codes
1. Low-Level Languages
- Machine Language uses binary (0 and 1)
- Assembly Code ( short codes)
2. High-Level Languages (e.g. C, Java, Python)
- Easy for human to read,
write, and understand Data Structure
Fundamental of Computer Programming

Syntax
- Rules defining how code should be written.

Semantics
- Meaning of the written code.

Data Structure
Fundamental of Computer Programming
Different ways to approach programming:

Procedural Programming (C, Pascal)


- Uses functions and procedures.

Object-Oriented Programming (OOP) (Java, Python, C++)


- Uses objects and classes.

Functional Programming (Haskell, Lisp)


- Uses mathematical functions

Data Structure
Fundamental of Computer Programming

Variables
- store values in memory.
- characteristics or attribute that can take on different
values or be measured.

Data types
- define the kind of data (e.g., int, float, string, boolean).

Data Structure
Fundamental of Computer Programming
Operators
- Used for computations:
Arithmetic Operators
- Basic Mathematical Calculations (+, -, *, /)
Relational Operators
- Comparing two values and return true or false (==, !=,
<, >)
Logical Operators
- Combining or modify Boolean expression (&&, ||, !)

Data Structure
Fundamental of Computer Programming

Functions
- Reusable blocks of code that perform a specific task.

Data Structure
Fundamental of Computer Programming
Program Development Process
1.Problem Definition – Understanding what needs to be solved.
2.Algorithm Design – Creating a step-by-step solution.
3.Coding – Writing the program using a programming language.
4.Compilation & Execution – Converting code into machine-readable form.
5.Testing & Debugging – Finding and fixing errors.
6.Maintenance – Updating and improving the program.

Data Structure
Fundamental of Computer Programming
Debugging and Error Handling
- Finding and fixing error (bugs) in code.
Syntax Errors
- Syntax violations that prevent code from executing
Logical Errors
- Incorrect logical flaws that leads to unintended behavior in
the program.
Runtime Errors
- Errors that occur while the program is running

Data Structure
Fundamental of Computer Programming

Computer Programming
Writing instruction or code that a computer can execute to
perform specific tasks.

Data Structure
Three Basic Programming Structures

Basic Programming Structures


1. Sequence Structure
2. Selection (Decision) Structure
3. Repetition (Loop) Structure

Data Structure
Three Basic Programming Structures

Sequence Structure
Simplest structure where the execution of code line by
line or one after another in a linear order.

Data Structure
Three Basic Programming Structures

Sequence Structure
print("Welcome to Python Programming")
name = input("Enter your name: ")
print(f"Hello, {name}! Have a great day!")

Data Structure
Three Basic Programming Structures

Selection (Decision) Structure


Deciding whether the given condition is true or false,
this involves decision-making using conditional
statement (if, if-else, elif)

Data Structure
Three Basic Programming Structures

Selection (Decision) Structure – If else


age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

Data Structure
Three Basic Programming Structures

Selection (Decision) Structure - elif


score = int(input("Enter your score: "))
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")

Data Structure
Three Basic Programming Structures

Repetition (Loop) Structure


a statement is repeated again and again until the
given condition is satisfied, using loops (for,
while)

Data Structure
Three Basic Programming Structures

Repetition (Loop) Structure – For Loop

for i in range(1, 6):


print(i)

Data Structure
Three Basic Programming Structures

Repetition (Loop) Structure – While Loop

num = 1 while num <= 5:


print(num)
num += 1

Data Structure
The Difference between Compilation and Interpretation

Compilation
- Translate the codes into machine
code (binary code)
- Errors are detected before the
execution.

Data Structure
The Difference between Compilation and Interpretation

Interpretation
- Translate and execute the source
code line by line.
- Errors are detected during
execution

Data Structure
Essential Differences between Python and Java

Both Python and Java are both popular programming languages.


Java
- Faster language
Python
- Simpler and easier to learn

Data Structure
Essential Differences between Python and Java

Java
- Syntax Strict with using curly braces {} and semicolons.
- Compiled into bytecode
- Executed by the Java Virtual Machine (JVM)
- High Performance, developing large-scale applications
like enterprise software.

Data Structure
Essential Differences between Python and Java

Python
- Simple and easy to read
- Interpreted languages (executed line by line)
- Best for automation, data science, AI, and
web development)

Data Structure
Essential Differences between Python and Java

Python
Print(“Hello, World!”)
Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!"); }}

Data Structure
Arrays (Lists, Tuple, Dictionary)

What is a List?
• Most basic structure for storing and accessing a collection of
data
• is a mutable sequence container that can
change size as items are added or removed.
• It is an abstract data type that is
implemented using an array structure to store the items
contained in the list
• They helped you keep related data together and perform the
same operations on several values at once.

DASTRUC
Arrays (Lists, Tuple, Dictionary)

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Add Items in Lists – using append

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Add Items in Lists – using insert

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Remove an Item from a List

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Change a Value from a list

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Combining Lists

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Access a List

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Access a List – Indexing and


Splitting

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Access a List – Indexing and


Splitting

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Access a List – Indexing and


Splitting

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Access a List – Indexing and


Splitting
• sub-list of the list using the following syntax.

list_variable(start:stop:step)

• The start denotes the starting index position of the list.


• The stop denotes the last index position of the list.
• The step is used to skip the nth element within a start:stop

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Access a List – Indexing and


Splitting

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Method
List – Built-In Function
Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified
value
extend() Add the elements of a list (or any iterable), to the end
of the current list
index() Returns the index of the first element with the
specified value
insert() Adds an element at the specified position

DASTRUC
Arrays (Lists, Tuple, Dictionary)

List – Built-In Function


Method Description

pop() Removes the element at the specified position

remove() Removes the first item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list

len() Calculate the length of the list.

min() Returns the minimum element of the list

max() Returns the maximum element of the list

DASTRUC
Arrays (Lists, Tuple, Dictionary)

A string is a sequence of characters and


can be thought of as a type of array.

DASTRUC
Arrays (Lists, Tuple, Dictionary)

What is a Tuple?
• Similar to lists – they allow you to display an
ordered sequence of elements.
• However, they are immutable, and you can’t
change the values stored in a tuple.

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Examples

DASTRUC
Arrays (Lists, Tuple, Dictionary)

What is a Dictionary?
• is used to store the data in a key-value pair format. These key-
value pairs offer a great way of organizing and storing data in
Python.
• an unordered collection of key-value pairs. It is a data structure
that allows you to store and retrieve values based on a unique
key
• is a mutable sequence container that can
change size as items are added or removed.

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Creating an empty dictionary and adding


values

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Creating an empty dictionary and adding


values

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Defining a dictionary and printing values

DASTRUC
Arrays (Lists, Tuple, Dictionary)

How to Access values in a dictionary

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Change Item Value in Dictionary

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Looping through the dictionary

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Dictionary – Built-In Function


Method Description
clear() Removes all the items from the dictionary
copy() Returns a shallow copy of the dictionary.
get() Returns the value for the specified key if the key is in the
dictionary, else returns the default value.
items() Returns a list of the dictionary's (key, value) tuple pairs.
keys() Returns a list of all the keys in the dictionary.
values() Returns a list of all the values in the dictionary
pop() Eliminates the element using the defined key

DASTRUC
Arrays (Lists, Tuple, Dictionary)

Dictionary – Built-In Function


Method Description

popitem() Removes the most recent key-value pair


entered
setdefault() Returns the value for the specified key if the
key is in the dictionary, else sets the key with
the default value and returns the default value.
update() Updates the dictionary with the key/value pairs
from the specified dictionary or an iterable of
key/value pairs.
len() Calculate the length of the dictionary.

del Deleting an item in a dictionary Ex:


del Employee["Name"]
DASTRUC
References
• https://www.geeksforgeeks.org/basics-of-computer-programming-for-beginners/
• https://www.techtarget.com/whatis/definition/algorithm
• https://www.geeksforgeeks.org/an-introduction-to-flowcharts/
• https://www.coursera.org/articles/what-is-programming
• https://www.coursera.org/articles/python-vs-
java?utm_medium=sem&utm_source=gg&utm_campaign=b2c_apac_x_coursera
_ftcof_career-academy_cx_dr_bau_gg_pmax_gc_s2_all_m_hyb_24-
08_x&campaignid=21573875733&adgroupid=&device=c&keyword=&matchtype=
&network=x&devicemodel=&creativeid=&assetgroupid=6544910561&targetid=&
extensionid=&placement=&gad_source=1&gclid=Cj0KCQjwhYS_BhD2ARIsAJTMM
QYWyjg2nBnY0U1r_u0_2ZVPlOaszB_-
Me9rdDPB_XAR5vFIDndUFP0aAvwzEALw_wcB
• https://dev.to/idurar/debugging-and-error-handling-mastering-the-art-of-
software-stability-15nh
END

You might also like