Professional Documents
Culture Documents
CS1010E
Programming Methodology
What is a Program?
• (Computer) Program
• A sequence of instructions telling a computer what to
do.
• Execution of a program
• Computer performs the instruction sequence.
• Programming language
• Language for writing instructions to a computer.
CS1010E Programming Methodology 4
Why Python?
• Clear and readable syntax
• Intuitive
• Natural expression
• Powerful
• Popular and Relevant
print('No')
CS1010E Programming Methodology 6
ELEMENTS OF A PYTHON
PROGRAM
CS1010E Programming Methodology 7
Variables
• A variable is a name you create in your program to
represent something, e.g. a value, a collection of value.
• In IDLE console, we can try the following example:
>>> my_int = 27
>>> my_int
27
Python Types
int 8 45 1234
float 2.3 3.14159
bool True False
str "cs1010s"
'cs1010s'
None Doesn’t matter if it’s
quote or double quote
CS1010E Programming Methodology 9
type() Function
>>> type(123)
<class 'int'>
>>> type('123')
<class 'str'>
>>> type(None)
<class 'NoneType'>
CS1010E Programming Methodology 10
Type conversion
>>> str(123)
'123'
>>> float('45.2')
45.2
>>> int(23.8)
23
>>> int('cs1010s')
ValueError!
CS1010E Programming Methodology 11
input() Function
>>> var = input('Please enter an integer: ')
>>> var
'65'
Suppose user enter 65
>>> var = int(var) through keyboard
>>> var
65
print() Function
>>> var = 65
>>> print(var)
65
>>> print('My variable has value:', var)
My variable has value: 65
Assignment
• An assignment statement stores a value or a
computational result in a variable.
SYNTAX
<variable> = <expression>
= is not equality,
but assignment
Example
>>> abc = 18 + 2
>>> my_string = 'This is my string'
>>> x, y = 1, 2
>>> x
1
>>> y
2
>>> y = y + 3
>>> y
5
CS1010E Programming Methodology 16
Arithmetic Operators
+ - * / ** // %
>>> a = 2 * 3 >>> 11 / 3
>>> a 3.6666666666666665
6
>>> 11 // 3
>>> 2 ** 3 3 Known as floor
8 division
>>> 11 % 3
23 Return
2 remainder
CS1010E Programming Methodology 17
Example
>>> 45/15/3 >>> 4 + 3**2
1 13
Logical Operators
or and not
A A
A or B A and B
True False True False
A not A
True False
False True
CS1010E Programming Methodology 20
Example
>>> 3>5 and 1<2
False
>>> a = True
>>> not a
False
CS1010E Programming Methodology 21
Strings
• A string is a sequence of characters enclosed in
single (or double) quotes, e.g. 'abcd'
• String operations:
1. We can index a string, e.g.
>>> s = 'abcd'
>>> s[0]
'a'
>>> s[1]
'b'
CS1010E Programming Methodology 22
Strings
• A string is a sequence of characters enclosed in
single (or double) quotes, e.g. 'abcd'
• String operations:
2. We can concatenate two strings, e.g.
>>> s = 'abcd'
>>> t = 'efg'
>>> s + t
'abcdefg'
CS1010E Programming Methodology 23
Strings
• A string is a sequence of characters enclosed in
single (or double) quotes, e.g. 'abcd'
• String operations:
3. We can compare two strings, e.g.
>>> s = 'abcd'
>>> t = 'efg'
>>> s < t
True
* Compare the 1st pair of characters first. If equal, compares the 2nd
pair of characters, … until a difference is found.
CS1010E Programming Methodology 24
PROBLEM SOLVING BY
PROGRAMMING
CS1010E Programming Methodology 29
Algorithm design:
What is known to you?
What is to calculate?
2a What do you need to know?
CS1010E Programming Methodology 31
Algorithm design:
r Pythagoras’ theorem:
r2 = a 2 + a 2 = 2 * a 2
2a Area of circle:
c = π * r2
= π * 2 * a2
CS1010E Programming Methodology 32
Algorithm:
r 1. Read length of square from user
2. Compute radius r: r2 = 2 * a2
2a
3. Compute area c = π * r2
4. Print out area of the circle
CS1010E Programming Methodology 33
# 3. Compute area
area = math.pi * radius_square