0% found this document useful (0 votes)
21 views35 pages

CE-111 Introduction To Computing For Civil Engineers

Uploaded by

Arifah
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)
21 views35 pages

CE-111 Introduction To Computing For Civil Engineers

Uploaded by

Arifah
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/ 35

CE-111

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… .

A window showing a list of folders should appear.


– To search any folder on the list, double click on the folder.
– Find the folder PythonLab and double click on it.
– In the box File name at the bottom of the window
1. Type HelloWorld.py
2. Then click on the button Save in the lower right corner of the window.
The title of the Editor should change to show the location of the file
HelloWorld.py
Lecture 04 12
Writing and running you first Program
• In the Editor type
# My first Python program
print("Hello World!")

left justified and on the first line.


Notice the colour coding: red for the comment, purple for the
function print() and green for the data "Hello World!".

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 (=)

“n is assigned the value 300.”

Can now be used in a statement or expression, and its value will be substituted

Variable name can be


displayed directly without
the need for print()

• Several variables can be assigned the same value (Chained Assignment)


Lecture 04 24
2.2.2 Variable Types in Python
• A variable may be assigned a value of one type and then later re-assigned a value of a different type

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.

There is no longer any


reference to the integer object
300. It is orphaned, and there
Lecture 04 is no way to access it. 26
2.2.4 Object Identity
• Every object that is created is given a number that uniquely identifies it.
• It is guaranteed that no two objects will have the same identifier during any period in which their
lifetimes overlap.
• Once an object’s reference count drops to zero and it is garbage collected, as happened to the 300
object above, then its identifying number becomes available and may be used again.
• The built-in Python function id() returns an object’s integer identifier.

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

Invalid variable names


• Can be descriptive

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

For floating-point numbers the value


stored internally for a float object may not
be precisely what you’d think it would be. Lecture 04 32
2.3.3 Logical Operators
Logical Expressions Involving
Boolean Operands

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

You might also like