Week3 - Chapter 3 Basic Pyhton
Week3 - Chapter 3 Basic Pyhton
Computer Programming 2
Wing IDE Personal 6.1 Inteface
Run
button
Write your code here
Computer Programming 3
The print Object
• Displaying/Prompting command for input gathering and displaying
output.
Computer Programming 4
The print Object
• This produces multiple line of output:
print (“I am ”)
print (“Iron Man”)
I am
Iron Man
Computer Programming 5
The print Object
• Another way of producing multiple-line text using \n
I am
Iron Man
Computer Programming 6
The output Object
• This produces single line of output
I am Iron Man
7
The output Object
Computer Programming 8
Computer Programming 9
Computer Programming 10
Variables and Literals
Variables
• Variable: a storage location in memory
11
Variables and Literals
Variables
Computer Programming 12
Variables and Literals
Literals
• Literal: a value that is written into a program’s code.
Computer Programming 13
Variables and Literals
Literals
15 is an integer literal
Computer Programming
14
Identifiers
• An identifier is a programmer-defined name for some part of a
program: variables, functions, etc.
• You cannot use any of the C++ key words as an
identifier. These words have reserved meaning.
Computer Programming 15
Variable Names
• A variable name should represent the purpose of the variable. For
example:
itemsOrdered
Computer Programming 16
Identifier Rules
•The first character of an identifier must be an
alphabetic character or and underscore ( _ ),
•After the first character you may use
alphabetic characters, numbers, or underscore
characters.
•Upper- and lowercase characters are distinct
Computer Programming 17
Rules of naming variables
Explanation Example
Variable name should START
either with letter of underscore. score, _number
Cannot start with number.
The reminding character CAN
consist of letters, numbers and total_sales, marks1
underscore.
Should NOT made of reserved and (reserved word),
words and contain any symbols. password& (contain symbol)
May NOT contain spaces of total sales (not valid since has
naming variable names. space between total & sales)
Names are CASE SENSITIVE with totalsales is not same with
uppercase and lowercase. Totalsales
Computer Programming 18
Valid and Invalid Identifiers
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes
total_Sales Yes
Computer Programming 19
Data Types
Numeric Data Type Character Data Type
int string (str)
float
long Bool Data Types
complex True
False
Computer Programming 20
Numeric Data Type - int
Computer Programming 21
Numeric Data Type - long
Computer Programming 22
Numeric Data Type - float
• E.g.
12.45 -3.8
Computer Programming 23
Numeric Data Type - complex
Computer Programming 24
Character Data Type - str
state = “Singapore”
print (state)
Computer Programming 25
bool Data Type
•Represents values that are true or false
•bool variables are stored as small integers
•false is represented by 0, true by 1:
bool allDone = true;
bool finished = false; allDone finished
1 0
Computer Programming 26
Variable Assignments
• An assignment statement uses the = operator to
store a value in a variable.
item = 12;
• This statement assigns the value 12 to the item
variable.
• The variable receiving the value must appear on
the left side of the = operator.
• This will NOT work:
// ERROR!
12 = item;
Computer Programming 27
Variable Initialization
•To initialize a variable means to assign it a
value and automatically defined with
appropriate data type
Computer Programming 28
Multiple Variable Assignment in the Same
Line
Computer Programming 29
Arithmetic Operators
Computer Programming 30
Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF
ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Computer Programming 31
A Closer Look at the / Operator
Computer Programming 32
Program Documentation / Comments
Computer Programming 33
Computer Programming 34
Computer Programming 35
Computer Programming 36
Computer Programming 37
Computer Programming 38
Computer Programming 39
Computer Programming 40
Computer Programming 41
Computer Programming 42
Computer Programming 43
Program to calculate area of rectangular
Computer Programming 44
And now??
Computer Programming 45
Let’s go for extra miles!
Change to calculate the width
required for an area of rectangular
Hint: ask the area and length
required from user.
Area = length*width
Computer Programming 46
Tutorial
write a program to calculate area and moment of
inertia for all these shape in one program
Computer Programming 47