CE-111 Introduction To Computing For Civil Engineers
CE-111 Introduction To Computing For Civil Engineers
Introduction to
Computing for Civil
Engineers
Lecture 3
Road Map for Today
• Introduction to Python
– What is Python
– Why use Python
– Typical uses of Python
– Limitations
– How to get Python
– Interacting with Python
– Using Python
Lecture 04 2
2. INTRODUCTION TO PYTHON
Lecture 04 3
2.1 What is Python?
• A computer language which
– is an interpreted,
– object-oriented,
– high-level programming language with dynamic semantics
– Opensource
• Its high-level built in data structures, combined with dynamic
typing and dynamic binding, make it very attractive
– for Rapid Application Development,
– as well as for use as a scripting or
– glue language to connect existing components together.
Lecture 04 4
2.2 Why Python?
• Easy to Learn and Use
• Mature and Supportive Python Community
• Support from Renowned Corporate Sponsors
• Hundreds of Python Libraries and Frameworks
• Versatility, Efficiency, Reliability, and Speed
• Big data, Machine Learning and Cloud Computing
• First-choice Language
• The Flexibility of Python Language
• Use of python in academics
• Automation
Lecture 04 5
Typical Uses of Python
• AI and machine learning. ...
• Data analytics. ...
• Data visualisation. ...
• Programming applications. ...
• Web development. ...
• Game development. ...
• Language development. ...
• Finance
Lecture 04 6
Limitations
• Performance and Speed. ...
• Incompatibility of Two Versions. ...
• Application Portability. ...
• Requires Additional Testing. ...
• Lacks Web Development Capabilities. ...
• Weak in Mobile Computing. ...
• Depends on Third-Party Frameworks and Libraries. ...
• No Option to Embed Block Comments.
Lecture 04 7
How to get Python?
Lecture 04 8
Interacting with Python
• The Python Interpreter
– Typical Python implementations
offer both an interpreter and
compiler
– Interactive interface to Python with
a read-eval-print loop
• IDLE Development Environment
– IDLE is an Integrated DeveLopment
Environ-ment for Python, typically
used on Windows
– Multi-window text editor with syntax
highlighting, auto-completion, smart
indent and other.
– Python shell with syntax highlighting.
– Integrated debugger with stepping,
persistent breakpoints, and call stack
visibility
Lecture 04 9
Python Scripts
• When you call a python program from the command line the
interpreter evaluates each expression in the file
• Familiar mechanisms are used to provide command line
arguments and/or redirect input and output
• Python also has mechanisms to allow a python program to act
both as a script and as a module to be imported and used by
another python program
Lecture 04 10
Getting Started
• Create a new folder in your disk space with the name PythonLab
• Launch the Python Integrated Development Environment (IDLE) -
begin with the Start icon in the lower left corner of the screen.
• If you are in a laboratory, search using the keyword Python and
click on IDLE (Python 3.x y-bit) .
A window with the title Python 3.x.x Shell should appear. This
window is the Shell.
• In the Shell click on File. A drop down menu will appear.
Click on New File. A window with the `title` Untitled should appear.
This window is the Editor.
Lecture 04 11
Getting Started (2)
• In the Editor, click on File, and then in the drop down menu click on Save As… .
13
Lecture 04 13
Writing and running you first Program (2)
• In the Editor click on Run, then on the drop down menu click on Run Module.
A window will appear with the message Source Must Be Saved. Click on OK.
Two things happen:
1. Your program is saved in the file HelloWorld.py
2. Then it is run to produce the text "Hello World!" in the Shell window. The text
in the Shell is blue to show that it is output. The screenshot below is an example
and its content may differ slightly to that in your Shell window.
Lecture 04 14
2.1 BASIC DATA TYPES IN PYTHON
Lecture 04 15
2.1.1 Integer
• There is effectively no limit to how long an integer value can be.
• Of course, it is constrained by the amount of memory your system has, as are all things, but beyond that an
integer can be as long as you need it to be.
• Python interprets a sequence of decimal digits without any prefix to be a decimal number
Lecture 04 16
2.1.2 Floating-Point Numbers
• The float type in Python designates a floating-point number.
float values are specified with a decimal point.
• Optionally, the character e or E followed by a positive or
negative integer may be appended to specify scientific
notation.
Lecture 04 17
2.1.3 Complex Numbers
• Complex numbers are specified as
– <real part>+<imaginary part>j.
Lecture 04 18
2.1.4 Strings
• Strings are sequences of character data.
• The string type in Python is called str.
• String literals may be delimited using either single or double
quotes. All the characters between the opening delimiter and
matching closing delimiter are part of the string.
Lecture 04 19
2.1.5 Built in functions
Lecture 04 20
2.1.6 Built-in functions
from Math library
• Besides (+, -, *, /, //, **, %, abs), we
have lots of other math functions
available in a math library.
• A library is a module with some
useful definitions/functions.
• To use a library, we need to make
sure this line is in our program:
import math
• Importing a library makes whatever
functions are defined within it
available to the program.
• Example for find square root:
>>import math
>>math.sqrt(4)
2
Lecture 04 21
Table of escape sequence
Lecture 04 22
2.2 VARIABLES
Lecture 04 23
2.2.1 Variable Assignment
• Variable: think as a name attached to a particular object
• needs not be declared or defined in advance.
• To create a variable, you assign it a value where the assignment is done with a single equals sign (=)
Can now be used in a statement or expression, and its value will be substituted
Lecture 04 25
2.2.3 Object References
• So the interpreter does the
• Python is a highly object- following:
oriented language. ― Creates an integer object
• Every item is an object of ― Gives it the value 300
specific type or class ― Displays it to the console
― Integer object is created
• A Python variable is a symbolic name that is a reference or pointer to an object.
This assignment creates an
integer object with the value verification
300 and assigns the variable
n to point to that object.
Lecture 04 27
2.2.5 Variable Names
• can be any length and can consist of
– uppercase and lowercase letters (A-Z, a-z),
– digits (0-9), and the underscore character (_).
– An additional restriction is that, although a variable name can contain digits, the first character of
a variable name cannot be a digit.
Legitimate variable names
• Case sensitive
• Underscore is
significant
Lecture 04 28
2.2.6 Reserved Words (keywords)
• No object can have the same name as a
reserved word.
• Type
help("keywords")
to the python interpreter
• Case-sensitive
• All entirely lowercase except False,
None, and True.
• Error when Creating a variable with the
same name as any reserved word.
Lecture 04 29
2.3 OPERATORS AND EXPRESSIONS IN PYTHON
Lecture 04 30
2.3.1 Arithmetic Operators
• operators are special
symbols that designate that
some sort of computation
should be performed.
• The values that an operator
acts on are called operands.
Lecture 04 31
2.3.2 Comparison Operators
Equality Comparison on
Floating-Point Value
Lecture 04 33
Lecture 04 34
2.3.4 Operator Precedance
• Operators at the top of the table have the lowest
precedence, and those at the bottom of the table
have the highest.
• Any operators in the same row of the table have
equal precedence.
• It is clear why multiplication is performed first in
the example above: multiplication has a higher
precedence than addition.
• Operator precedence can be overridden using
parentheses. Expressions in parentheses are always
performed first, before expressions that are not
parenthesized.
There is nothing wrong with making liberal use of
parentheses, even when they aren’t necessary to
change the order of evaluation.
Lecture 04 35