0% found this document useful (0 votes)
7 views

Week3 Chapter3

The document discusses Python programming basics like variables, data types, operators and Wing IDE. It explains concepts such as defining variables, assigning values, different data types (numeric, string, boolean), arithmetic operators and how to write simple programs in Wing IDE.

Uploaded by

Fatyn Razs II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Week3 Chapter3

The document discusses Python programming basics like variables, data types, operators and Wing IDE. It explains concepts such as defining variables, assigning values, different data types (numeric, string, boolean), arithmetic operators and how to write simple programs in Wing IDE.

Uploaded by

Fatyn Razs II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CHAPTER 3

GETTING START WITH PYTHON


PROGRAMMING LANGUAGE USING
WING IDE PERSONAL 6.1
Example of Simple Calculator Program
using Python Language
2

# This program is to make a simple calculator comment

print (“Additional Calculator of Two Input Number")


print ("")
Display
prompt
num1 = float (input("Enter first number: ")) Gathering input
num2 = float (input("Enter second number: ")) prompt
sum=num1+num2 Process statement
print (num1, "+", num2, "=", sum) Displaying output

Computer Programming
Wing IDE Personal 6.1 Inteface
3

Run button

Write your code here

Your program execute here

Computer Programming
The print Object
4

 Displaying/Prompting command for input gathering


and displaying output.

 Prompting display statement is so easy using Python

print (“Hello World”)

Computer Programming
The print Object
5

 This produces multiple line of output:

print (“I am ”)
print (“Iron Man”)

I am
Iron Man

Computer Programming
The print Object
6

 Another way of producing multiple-line text using


\n

print (“I am\nIron Man ”)

I am
Iron Man

Computer Programming
Classes and Objects
7
The cout Object
 This produces single line of output

print (“I am ”)print (“Iron Man”)

I am Iron Man
Variables and Literals
8
Variables
 Variable: a storage location in memory

 Has a name and a type of data it can hold


 Python allow programmer use variable without declaring
it.
 Can directly assigned/initialized any value into those
variables.
 Automatically select appropriate data type according to
the value.

num1 = 10 Variable automatically assigned as int data type


Variables and Literals
9
Variables

Way of declaring variable


while asking for input.
Variable num1 assigned to
hold float data type

Computer Programming
Variables and Literals
10
Literals
 Literal: a value that is written into a program’s code.

"hello, there" (string literal)


12 (integer literal)

Computer Programming
Variables and Literals
11
Literals

15 is an integer literal

This is a string literal


Output Display

Total number of green apple is 15

Computer Programming
Identifiers
12

 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
Variable Names
13

 A variable name should represent the purpose of


the variable. For example:

itemsOrdered

The purpose of this variable is to hold the number


of items ordered.

Computer Programming
Identifier Rules
14

 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
Rules of naming variables
15

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), password&
words and contain any symbols. (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
Valid and Invalid Identifiers
16

IDENTIFIER VALID? REASON IF INVALID

totalSales Yes

total_Sales Yes

total.Sales No Cannot contain .

4thQtrSales No Cannot begin with digit

totalSale$ No Cannot contain $

Computer Programming
Data Types
17

Numeric Data Type Character Data Type


 int  string (str)

 float

 long Bool Data Types


 complex  True

 False

Computer Programming
Numeric Data Type - int
18

 Plain integers of positive or negative whole numbers


 E.g. : 10, -10
 Assign for suitable variable application, such as
number of student (num_student), number of car
(num_car) and etc.

num_student = int (input(“Enter number of student =“)

Computer Programming
Numeric Data Type - long
19

 Long integer with infinite size


 Similar with int, except the are followed by letter
“L”
 E.g. : 10L, -10L

Computer Programming
Numeric Data Type - float
20

 Represent real numbers.

 E.g.
12.45 -3.8

 Stored in a form similar to scientific notation (6.022e23)

 All floating-point numbers are signed

Computer Programming
Numeric Data Type - complex
21

 Use to represent complex number


 Represent by formula a+bi, where a and b are
floats, while i is the  1
 E.g. : 10+28i

Computer Programming
Character Data Type - str
22

 Use to represent complex number


 Represent by formula a+bi, where a and b are
floats, while i is the  1

state = str (input("Enter State: "))


print (“State is: ", state)

state = “Singapore”
print (state)

Computer Programming
bool Data Type
23

 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
Variable Assignments
24

 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
Variable Initialization
25

 To initialize a variable means to assign it a value


and automatically defined with appropriate data
type

length = 12; #Integer data types

Computer Programming
Multiple Variable Assignment in the
26
Same Line

Computer Programming
Arithmetic Operators
27

 Used for performing numeric calculations


 C++ has unary, binary, and ternary operators:
 unary (1 operand) -5
 binary (2 operands) 13 - 7
 ternary (3 operands) exp1 ? exp2 : exp3

Computer Programming
Binary Arithmetic Operators
28

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
A Closer Look at the / Operator
29

 / (division) operator performs integer division if


both operands are integers
cout << 13 / 5; // displays 2
cout << 91 / 7; // displays 13
 If either operand is floating point, the result is
floating point
cout << 13 / 5.0; // displays 2.6
cout << 91.0 / 7; // displays 13.0

Computer Programming
A Closer Look at the % Operator
30

 % (modulus) operator computes the remainder


resulting from integer division
cout << 13 % 5; // displays 3
 % requires integers for both operands
cout << 13 % 5.0; // error

Computer Programming
Comments
31

 Used to document parts of the program


 Intended for persons reading the source code of
the program:
 Indicate the purpose of the program
 Describe the use of variables

 Explain complex sections of code

 Are ignored by the compiler

Computer Programming
Comments
32
Single Line
Begin with // through to the end of line:
int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area

// calculate rectangle area


area = length * width;

Computer Programming
Comments
33
Multi-Line Comments
 Begin with /*, end with */
 Can span multiple lines:
/* this is a multi-line
comment
*/
 Can begin and end on the same line:
int area; /* calculated area */

Computer Programming
34

Thank You
Q&A

Computer Programming

You might also like