Link Search Menu Expand Document

Module 02. Getting Started with Python

Lecture Date 1: August 30, 2021 - Monday Slides-1
Lecture Date 2: September 1, 2021 - Wednesday Slides-2 - Slides-3
DataCamp DataCamp Chapter: Introduction to Python -> Python Basics

In this module, we will start with installing Python using the Anaconda distribution. We will then talk about variables and basic data types in Python. Detailed code examples and videos can be accessed using the following list.

Table of contents:

1. Variables and Basic Data Types in Python

Download code

1.1. Numbers

1.1.1. Integers

-1
-1
type(-1)
int
9
9
8234118
8234118
12312741717471289549
12312741717471289549
9999999999999999999999999999999
9999999999999999999999999999999

1.1.2. Floats

1.0
1.0
type(1.0)
float

Python Tip: In a line, any code that comes after square symbol (#) is ignored.


4/3  # a real number with infinite decimal points
1.3333333333333333
3/5
0.6
4.12434351e-10  # scientific notation
4.12434351e-10
2.1e5  # another scientific notation
210000.0

1.1.3. Complex numbers

3+5j
(3+5j)
type(3+5j)
complex
complex(3)
(3+0j)
complex(0,5)
5j

1.1.4. Calculations

Basic arithmetic

+ addition, - subtraction, * multiplication

5 + 9
14
2.3 + 7.7
10.0
0.1 + 0.2
0.30000000000000004
99-11
88
6 * 4
24
0.1 * 99
9.9

/ decimal division, // integer division, % modulus, ** power

5.0 / 2
2.5
5 / 2
2.5
5 // 2
2
5.0 // 2
2.0
9 % 2
1
9.9 % 3
0.9000000000000004
2 ** 3
8
5.5 ** 3.3
277.457759723262

1.1.5. Operator Precedence

PrecedenceOperator
Highest precendence**
….* / // %
Lowest precendence+ -

Python Tip: Parentheses overrides the above precedence.

Python Tip: If multiple operators have the same precedence, code is executed from left to right.


5 + 3 * 8 
29
(5 + 3) * 8 
64
2 + 10 / 2 * 5
27.0
2 + 32 / 2 ** 5
3.0

1.2. Mathematical functions

Built-in functions: abs and round

abs(-123)
123
abs(2)
2
abs(3+4j)
5.0
round(3.4)
3
round(3.7)
4
round(3.5)
4
round(-3.5)
-4
round(-2.5)
-2

Python Tip: In Python 3, rounding a decimal number, which is midway through two integers, gives the even number.


import math
math.ceil(3.7)
4
math.factorial(5)
120
math.exp(2)
7.38905609893065
math.log10(10)
1.0
math.sqrt(100)
10.0
math.cos(0)
1.0
math.pi
3.141592653589793
math.e
2.718281828459045
math.sin(math.pi/2)
1.0

1.3. Variables

abc = 637.3276
efg = 5685
xyz = abc - efg
print(xyz)
jkl = 35746.78234
print(xyz / jkl)
-5047.6724
-0.14120634276925526

List of names you cannot use

import keyword
keyword.kwlist
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'async',
 'await',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']

2. Simple Physics Models

Download code

2.1. Linear Motion Example

x0 = 0
x_end = 10
vx = 1.3
t = (x_end-x0)/vx
print("Time it takes to move from x1 to x2 is ", t, "seconds") 
Time it takes to move from x1 to x2 is  7.692307692307692 seconds

2.2. Free Fall Example

predicting position

y1=50 # initial position
vy=0 # initial velocity
t=3 # time
g=9.8 # gravity

new_position = y1+vy*t-0.5*g*t**2
print("The new position after free fall is ", new_position) 
The new position after free fall is  5.899999999999999

Predicting time

y1 = 50
y2 = 0
vy = 0
g = 9.8
q = vy**2 + 2*g*(y1-y2)
t1 = (-vy + q**0.5)/-g 
t2 = (-vy - q**0.5)/-g
print (t1, t2)
3.1943828249996993 -3.1943828249996993

3. Example Questions and Solutions


Back to top

Copyright © Hamdi Kavak. CDS 230 - Modeling and Simulation 1.