Getting Started with Python
A program can be typed directly in a shell or stored in a file
that is read into the shell and evaluated.
IDLE - python shell (interactive interpreter)
IDLE - text editor (Run Run Module)
PyCharm IDE
Spyder IDE
Visual Studio Code IDE
PyDev - Eclipse IDE
Fundamentals of Computer & Programming 45
Objects
programs manipulate data objects
objects have a type that defines the kinds of things
programs can do to them
objects are
scalar (cannot be subdivided)
non-scalar (have internal structure that can be accessed)
Fundamentals of Computer & Programming 46
Scalar Objects
int – represent integers, ex. 5
float – represent real numbers, ex. 3.27
bool – represent Boolean values True and False
NoneType – special and has one value, None
can use type() to see the type of an object
>>> type(5)
int
>>> type(3.0)
float
Fundamentals of Computer & Programming 47
Type Conversions (Cast)
can convert object of one type to another
float(3) converts integer 3 to float 3.0
int(3.9) truncates float 3.9 to integer 3
>>> float(3)
3.0
>>> int(3.9)
3
Fundamentals of Computer & Programming 48
Printing to console
to show output from code to a user, use print command
Shell only:
>>> 3+2
5
Shell and File:
print(3+2)
5
Fundamentals of Computer & Programming 49
Expressions
combine objects and operators to form expressions
an expression has a value, which has a type
syntax for a simple expression
<object> <operator> <object>
4 * 3
4 * 3 + 9
4 * 3 + 9 / 5
Fundamentals of Computer & Programming 50
Operators on Ints and Floats
i+j the sum if both are ints, result is int
i-j the difference if either or both are floats, result is
float
i*j the product
i/j division result is float
i%j the remainder when i is divided by j
i**j i to the power of j
i//j integer division
Fundamentals of Computer & Programming 51
Operators on Ints and Floats
Fundamentals of Computer & Programming 52
Simple Operations
parentheses used to tell Python to do these operations
first
operator precedence without parentheses
highest precedence at top, lowest at bottom
same row means same priority
• they executed left to right, as appear in expression
**
* / // %
+ -
Fundamentals of Computer & Programming 53
Simple Operations
4-5+6**2/4**3%7
((4-5)+(((6**2)/(4**3))%7))
** (1) ** (2)
/ (3)
%(4)
- (5)
+ (6)
Fundamentals of Computer & Programming 54
Binding Variables and Values
• equal sign is an assignment of a value to a variable name
pi = 3.14159
1
pi_approx = 22/7
value stored in computer memory
an assignment binds name to value
retrieve value associated with name or variable by invoking the
name, by typing pi
Fundamentals of Computer & Programming 55
Binding Variables and Values
Fundamentals of Computer & Programming 56
Abstracting Expressions
why give names to values of expressions?
to reuse names instead of values
easier to change code later
pi = 3.14159
radius = 2.2
area = pi*(radius**2)
Fundamentals of Computer & Programming 57
Changing Bindings
can re-bind variable names using new assignment
statements
previous value may still stored in memory but lost the
handle for it
value for area does not change until you tell the computer
to do the calculation again
3.14
pi = 3.14 pi pi
2.2
radius = 2.2 radius
area = pi*(radius**2) area 3.2
radius = radius+1 15.1976
Fundamentals of Computer & Programming 58
Question
Which is allowed in Python?
a) x + y = 2
b) x*x = 2
c) 2 = x
d) xy = 2
Fundamentals of Computer & Programming 59
Question
1) e = a + (b * c) / d
2) e = (a + b) * c / d
3) e = ((a + b) * c) / d
4) e = (a + b) * (c / d)
Fundamentals of Computer & Programming 60
Question
usa_gold = 46
uk_gold = 27
romania_gold = 1
total_gold = usa_gold + uk_gold + romania_gold
print(total_gold)
romania_gold = romania_gold + 1
print(total_gold)
Fundamentals of Computer & Programming 61
output:
Question
74
74
74
75
Fundamentals of Computer & Programming 62