Chapter Two Introduction To Python Aman W Department of Applied Physics University of Gondar
Chapter Two Introduction To Python Aman W Department of Applied Physics University of Gondar
Chapter Two Introduction To Python Aman W Department of Applied Physics University of Gondar
Introduction to python
Aman W
Department of Applied Physics
University of Gondar
Objective
Introduction
What is Computation?
Introduction to Console vs. GUI
Basics of Python scripting
Variables and Data types
Assignments of variables
Expressions
Control structures
Flow Control: Looping
Conditionals
Functions
Exception
Flow Control: Looping
For & while loop
Introduction
Basically a computer
Performs calculations and
Remember/store their results
To perform the above activities, a computer may use
Built in primitives or
Building your own methods(function, procedures, modules) of
calculating that parameter
Example
Playing chess
Calculating temperature/pressure of the atmosphere/
Getting the speed of sound in water/gas
etc
What is computation?
Can you use this technique for calculating the square root of x
Imperative knowledge
and, exec, not, assert, finally, or, break, for, pass, class, from,
print, continue, global, raise, def, if, return, del, import, try,
elif, in, while, else, is, with, except, lambda, yield.
Python Identifiers
What is a variable?
Variables: Alphanumeric (letters and numbers) identifiers
that hold values, like integers, strings of characters, and
dates.
Value: The content of some variable.
Examples of Variables
myAge= 29
The variable myAge might hold the value 29.
yourName=Adane
The variable yourName might hold the value Adane
Assigning Variables
>>> a = 4
b =sally b S
a
We can view an assignment as a l
write to memory l
The variable represents a y
container in memory
Assignment places, or writes the
value into that container
Assignments of variables
A variable is a named place in the memory where a
programmer can store data and later retrieve the data
using the variable name
Programmers get to choose the names of the variables
The declaration happens automatically when you assign a
value to a variable.
The equal sign (=) is used to assign values to variables.
You can change the contents of a variable in a later
statement
x = 12.2 x 12.2=100
y = 14
y = 14
x = 100
Assignment with expression
x = 3.9 * x * ( 1 - x )
A variable is a memory
location used to store a
X= 0.6
value (x=0.6).
0.6
0.6
x = 3.9 * x * ( 1 - x )
What is an operator?
Simple answer can be given using expression
4 + 5 is equal to 9.
Here 4 and 5 are called operands and + is called operator.
Python language supports following type of operators.
Arithmetic / Mathematical Operators
Comparision Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Logical Operators; Boolean
>>> x = 1 + 2 ** 3 / 4 * 5
1 + 2 ** 3 / 4 * 5
>>> print x
11
>>> 1+8/4*5
Parenthesis
Note 8/4 goes 1+2*5
before 4*5 because of the
Power left-right rule.
Multiplication
Addition
Left to Right 1 + 10
11
Comparison Operators
== Checks if the value of two operands are equal or not, if yes then
condition becomes true. (a == b) is not true.
!= Checks if the value of two operands are equal or not, if values are not
equal then condition becomes true. (a != b) is true.
> Checks if the value of left operand is greater than the value of right
operand, if yes then condition becomes true. (a > b) is not true.
< Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true. (a < b) is true.
>= Checks if the value of left operand is greater than or equal to the value
of right operand, if yes then condition becomes true. (a >= b) is not
true.
<= Checks if the value of left operand is less than or equal to the value of
right operand, if yes then condition becomes true. (a <= b) is true.
Control Structures
==
Start
while x != n:
X=0 >
<
Execute Command
Getting Python:
The most up-to-date and current source code, binaries,
documentation, news, etc. is available at the official website of
Python:Python Official Website : http://www.python.org/
Documentation
You can download the Python documentation from the following site.
The documentation is available in HTML, PDF, and PostScript formats:
http://docs.python.org/index.html
Tutorial
You should definitely check out the tutorial on the Internet at:
http://docs.python.org/tutorial/.
Assignment 1