0% found this document useful (0 votes)
13 views

Lec Intro Computing

Uploaded by

Rebecca Morris
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)
13 views

Lec Intro Computing

Uploaded by

Rebecca Morris
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/ 40

ENGG1810/9810

ENGG1810/9810
Introduction to Engineering Computing
Lecture 1: Introduction to Programming (Python)

Dr. Imdad Ullah


Faculty of Engineering
ENGG1810/9810

COMMONWEALTH OF AUSTRALIA

Copyright Regulations 1969

WARNING

This material has been reproduced and communicated to you by or on behalf of


the University of Sydney pursuant to Part VB of the Copyright Act 1968 (the Act).

The material in this communication may be subject to copyright under the Act.
Any further reproduction or communication of this material by you may be the
subject of copyright protection under the Act.

Do not remove this notice.


INTRODUCTION
ENGG1810/9810

What will you learn in this course?


Week 1: Introduction to Python

Week 2: Storing Data and Making Decisions Programming


Week 3: Repeating Actions I Basics

Week 4: Repeating Actions II

Week 5: Functions I
Week 6: Functions II Functions
Week 7: Libraries and Modules I
and Packages

Week 8: Libraries and Modules II

Week 9: Application I

Week 10: Application II Advanced


Week 11: Case Study I
Topics

Week 12: Case Study II

Week 13: Revision and Exam Guide


ENGG1810/9810

Today’s Lecture

• Why Engineers Must Learn Programming?


• Why Python?
• Python Basics
• Mathematical Operations
• Python Variables and Types
• Built-in Functions – print(), int(), str(), input()
ENGG1810/9810

Why Should Engineers LEARN to </CODE>?

Work Faster and More Efficient


You can work 10 times faster and more efficient by writing computer programs to automate
tedious tasks (e.g. data cleaning and integration) that you would otherwise need to do by hand.

Discover Creative Solutions


Programming allows you to discover more creative solutions than your colleagues who don't
know how to program. It lets you go beyond simply using the tools and data sets that everyone
else around you uses; implement far more sophisticated analysis
ENGG1810/9810

Why Should Engineers LEARN to </CODE>?

Biomedical engineering Civil engineering

Aeronautical engineering Mechatronic Engineering

The Job Market needs people have experience


with programming languages (Python, etc.).

Electrical Engineering Software engineering

Chemical and Mechanical engineering


biomolecular engineering
ENGG1810/9810

Why Python?
Python is an interpreted high-level general-purpose programming language,
designed by Guido Van Rossum

1989 In December, Van Rossum had been looking for a 'hobby' programming project during Christmas
as his office was closed when he decided to write an interpreter for a "new scripting language”.

He named "Python" to "being in an irreverent mood (and a big fan of Monty Python's Flying Circus)"

1999 Van Rossum submitted a funding proposal to DARPA called "Computer Programming for Everybody",
in which he further defined his goals for Python:
1. Code that is as understandable as plain English
2. An easy and intuitive language just as powerful as major competitors
3. Open source, so anyone can contribute to its development
4. Suitability for everyday tasks, allowing for short development times
ENGG1810/9810

Why Learn Python?


Code that is as understandable as plain English (human-readable)
Human: If there is 4 in 1,2,3,4, please print “There is 4”

Python: if 4 in [1,2,3,4]: print(“There is 4")

An easy and intuitive language just as powerful as major competitors


Python was built with the goal of getting rid of the complexity and keeping only the necessary.
It is easier to read, write, and learn than most other major programming languages.
Above code written in Java: (quite long code and extra formatting)
int arr[] = {1,2,3,4};
int toCheckValue = 4;
for (int element : arr) {
if (element == toCheckValue) {
System.out.println(”There is ”+toCheckValue);
break;
}
}
ENGG1810/9810

Why Learn Python?


Open source, so anyone can contribute to its development
Python is free, open and multiplatform; not all languages can boast that. Python offers a
rich ecosystem of packages held within The Python Package Index (PyPI). Users can build
modules for the ever-growing PyPi library.

Suitability for everyday tasks, allowing for short development times


Python serves as a general-purpose language, which can process virtually any task.
Python is used in Data Mining, Data Science, AI, Machine Learning, Web Development, Web
Frameworks, Embedded Systems, Graphic Design applications, Gaming, Network development,
Product development, Rapid Application Development, Testing, Automation Scripting, etc.
ENGG1810/9810

Why Learn Python?


Python is used in the world’s largest and most sophisticated companies

Python is the fastest growing programming language


ENGG1810/9810

Today’s Lecture

• Why Engineers Must Learn Programming?


• Why Python?
• Python Basics
• Mathematical Operations
• Python Variables and Types
• Built-in Functions – print(), int(), str(), input()
INTRODUCTION
ENGG1810/9810

Let’s Start with Python Basic!


ENGG1810/9810

Python Basics

• Python is an interpreter language, which means it executes the code line by line.
• can be typed 1) directly in a shell or 2) stored in a .py file that is read into the shell and evaluated

• Python provides an Python Interpreter: Shell/REPL, for executing a single Python command and
display the result.

Let’s Start with Python Interpreter: Shell/REPL

62
• Q: Why called Shell?
A: The “Shell” covers/wraps a kernel and allows interacting a user’s input. User
Kernel: A computer program at the core of a computer's operating system and has complete control over everything in the system
HW: hardware (CPU, RAM, Disks, Network Ports)

• Q: Why REPL (Read, Evaluate, Print, Loop)?


A: It reads the command, evaluates the command, prints the result, and loops it back to read the
command again.
ENGG1810/9810

Python Basics

Where can we find the Python Interpreter: Shell/REPL?


Go to Ed à Workspaces à Click à Title Lecture1 à click à Type python and Press

Or Go to Python Official Website https://www.python.org/ à Click the yellow button


ENGG1810/9810

Python Basics

Let’s try some Basic Math (addition, subtraction, multiplication, division)

1. Addition
and press

2. Subtraction

3. Multiplication
We are currently using python 3
but python 2 would produce the
following outcome:

4. Division
ENGG1810/9810

Python Basics: Operators

Arithmetic Operators
Operator Operation Example Result
+ Addition 2+1 3
- Subtraction 6.3 – 2.1 4.2
* Multiplication 3*4 12
/ True Division 6/3
oo 2
% Remainder (modulo) 5/3 2
** Exponentiation 2**3 8
// Floor division 20//3 6
ENGG1810/9810

Python Basics: Programming


Human Language VS Programming Languages
• Human Language (English): Words
• Programming Language: Numbers, Strings, Simple Operators

English: Can you write your phone number backwards?

Python:

How can we manipulate Numbers, Strings, and Simple Operators?


ENGG1810/9810

Python Basics: Data Types

Objects are Python's abstraction for data. In Python, data are represented by objects or by
relations between objects.

Data Types
Data types are the classification or categorization of data items.
Numbers
• Integers – whole numbers
• Float – numbers with decimal points
• Complex number – real and imaginary numbers
Texts
• String - combination of any characters that appear on keyboard e.g. hello world, Nut3lla#

Boolean – True/False options

*Use the type() function to get the type or class name of an object.
ENGG1810/9810

Python Basics: Variables and Data Types


Thus, all values are actually an object of a class depending upon the value.

A literal value is assigned to a variable using the “=” operator;


• The left side should be the name of a variable,
• Use meaningful names
• Should not have space in them
• Must not use any reserved words (e.g. if, for, True, etc.)
• The right side should be a value.
• Can be numbers, text, Boolean

The following assigns a name to an integer value:


my_age = 20
Variable name Value

Variables in Python are names given to objects, so that it becomes easy to refer a value.
In other words, a variable points to an object.
ENGG1810/9810

Python Basics: Variables and Data Types


A literal value is assigned to a variable using the “=” operator;
• the left side should be the name of a variable,
• the right side should be a value.

ENGG1810 e-Shopping Mall


ENGG1810/9810

Python Basics: Variables and Data Types


A literal value is assigned to a variable using the = operator;
• the left side should be the name of a variable,
• the right side should be a value.

ENGG1810 e-Shopping Mall

ps5 = 1500 bose_sbar = 700


ENGG1810/9810

Python Basics: Variables and Data Types


(Numbers)

Q: Caren wants to buy both PS5 and Bose Soundbar.


How much does she need to pay?
ENGG1810/9810

Python Basics: Variables and Data Types


(Numbers)

Q: Caren wants to buy only PS5 as she can receive


10% off on PS5. How much does she need to pay?

* You can also try, ps5 * (1 - 0.10) or others.

You can also update the value of the PS5.

or
ENGG1810/9810

Python Basics: Variables and Data Types


(Text)

Q: Caren wants to send the text message to her friend


(Tom) in order to let her know what products are available
in the ENGG1810 e-Shopping Mall.
Note: we need to send the exact product name.

ps5_name = ‘PLAYSTATION 5 GAMING CONSOLE’ The ‘+’ operator


concatenates two
bose_sbar_name = ‘BOSE Soundbar 700’
strings together.
ENGG1810/9810

Python Basics: Variables and Data Types


(Text)

Q: Caren wants to send the text message to her friend


(Tom) in order to let her know what products are available
in the ENGG1810 e-Shopping Mall.
Note: we need to send the exact product name.

ps5_name = ‘PLAYSTATION 5 GAMING CONSOLE’

bose_sbar_name = ‘BOSE Soundbar 700’


ENGG1810/9810

Python Basics: Variables and Data Types


(Text)

Q: Caren wants to send the text message to her friend


(Tom) in order to let her know what products are available
in the ENGG1810 e-Shopping Mall.
Note: we need to send the exact product name.

ps5_name = ‘PLAYSTATION 5 GAMING CONSOLE’

bose_sbar_name = ‘BOSE Soundbar 700’


ENGG1810/9810

Python Basics: Variables and Data Types


(Boolean: True/False Option)

Q: Tom asked whether PS5 would be cheaper than


the Soundbar if we receive 60% off on PS5. Caren
guessed that it would be cheaper. Is this correct?
ENGG1810/9810

Python Basics: Operators

Relational Operators
Operator Operation Example Result
< Less than 3<5 True
> More than 3>5 False
<= Less than or equal to 3 <= 5 True

>= Greater than or equal to 5 >= 5 True

== Equal/Same as 5 == 3 False
!= Not equal to 5 != 3 True

Important Note!
• Symbol == checks whether the left-side value is equal to the right-side value
• Symbol = assigns value (right-side) to variable (left-side).
ENGG1810/9810

Today’s Lecture

• Why Engineers Must Learn Programming?


• Why Python?
• Python Basics
• Mathematical Operations
• Python Variables and Types
• Built-in Functions – print(), int(), str(), input(), str.format()
ENGG1810/9810

Try Built-in Functions


The Python interpreter has a number of functions and types built into it that are always available

Wait, what is a function?


absolute
Function is a block of re-usable
codes that executes when it is
requested.

https://docs.python.org/3/library/functions.html
ENGG1810/9810

Try Built-in Functions with .py file


Let’s try to run a .py file that is read into the shell
In your workspace (opened in the slide 14), click New File à Set the file name as
*you can use any name

Editor

If you cannot see this terminal, please


click the following button:
Terminal
ENGG1810/9810

Python Built-in Functions: print()

Same as the slide 22.


Q: Caren wants to buy both PS5 and Bose Soundbar.
How much does she need to pay?

Print a message onto the screen


ENGG1810/9810

Python Built-in Functions: print()

Same as the slide 26.


Q: Caren wants to send the text message to her friends
(Tom) in order to let her know what products are available
in the ENGG1810 e-Shopping Mall.
Note: we need to send the exact product name.

ps5_name = ‘PLAYSTATION 5 GAMING CONSOLE’

bose_sbar_name = ‘BOSE Soundbar 700’


Print a message onto the screen
ENGG1810/9810

Python Built-in Functions: int()


The function int(arg, base)to return an integer.

Use function int(arg, base)to return an integer.


•arg is the number or string representing an integer literal.
•arg default value is 0, base default value is 10.

Default value is returned if no arguments is provided.


If argument is a float, the value is truncated to its whole number.
ENGG1810/9810

Python Built-in Functions: str()


The str() function converts the specified value into a string.

0401102345
ENGG1810/9810

Python Built-in Functions: str.format()


Another way of displaying a string without casting numeric variables to string (str()).

arguments

string object function

• Curly brackets {} are placeholders.


• First {} replaced by value contained in variable country
• Second {} replaced by value contained in variable code
• Sequence of arguments matters in parenthesis.
• Number of curly brackets must match number of arguments passed into function.
ENGG1810/9810

Python Built-in Functions: str.format()


Another way of displaying a string without casting numeric variables to string (str()).

arguments

string object function


Escape sequence
• Curly brackets
• When{} are
a placeholders.
backslash (\) appears in a string, it’s known as the
• First {}escape
replacedcharacter.
by value contained in variable country
• Second {} replacedand
• Backslash by value contained immediately
the character in variable code
following it form an
• Sequence of escape
arguments matters
sequence. in parenthesis matters.
• Number of curly brackets must match number of arguments passed into function.
ENGG1810/9810

Python Built-in Functions: str.format()


We can specify where to put each argument as well.

argument 0 argument 1

string object function

• This will return the same output as before but parameter index is stated:
• {0} replaced by value contained in variable code
• {1} replaced by value contained in variable country
• Parameter index can be used if you want to print the argument list in different order
e.g. {1}{0} to print value of country then code.
• Contents outside the curly bracket is printed as it is.
• Parameter index starts with 0.
ENGG1810/9810

Python Built-in Functions: input()


Python allows for user input. That means we are able to ask the user for input.

Use function input(prompt)to get user input from keyboard.


•Argument prompt is the text displayed on the terminal
•Value returned by this function is always a string.
ENGG1810/9810

THANKS FOR
WATCHING
Good luck in studying

You might also like