0% found this document useful (0 votes)
10 views25 pages

Chapter4 Programmingelement

Uploaded by

muhammad hazeq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
10 views25 pages

Chapter4 Programmingelement

Uploaded by

muhammad hazeq
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

CHAPTER 4:

PYTHON
PROGRAMMING
ELEMENT
VARIABLE AND RESERVE WORDS

• To construct a basic program normally comprised two element (1) Variable


and (2) Keywords
• Variables are defined as a memory location that will hold or retain any input
values or data that might be altered throughout the execution of the
computer program.
• Another name for variable is programmer-defined identifier. It will create a
space with special name that has been assigned by the programmer, together
with its data types.
Rules and Guideline of naming variables
Guidelines Examples
A variable name should START either with score, _number
a letter or an underscore. It cannot start
with a number.
The remaining characters CAN consist of total_sales, marks
letters, numbers and underscores.
It should NOT be made of reserved words and(reserved words), passwords$
and contain any symbol (contain symbol)
May NOT contain spaces of naming total sales (not valid since has space
variable names between total and sale)
Names are CASE SENSITIVE with Totalsales is not the same with totalsales
uppercase and lowercase
• Keywords or reserve words are the terminologies which refer to specific and
special texts in any programming languages that perform specific task in the
development of computer program.
• All listed keywords cannot be used to name any variable or identifier
VALID AND INVALID VARIABLES/IDENTIFIER
IDENTIFIER VALID? REASON IF INVALID

totalSales Yes

total_Sales Yes

total.Sales No

4thQtrSales No

totalSale$ No
DATA TYPE
• Data types in programming can be defined as a collection of
data values and a set of predefined operators on those
Numeric

Data type
values.
• Based on the data type of a variable, the interpreter
allocated memory and decides what can be stored in the
reserved memory.
String
Boolean
NUMERIC DATA TYPE-INT
• 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 =“))


NUMERIC DATA TYPE-LONG

• Long integer with infinite size


• Similar with int, except the are followed by letter “L”
• E.g. : 10L, -10L
NUMERIC DATA TYPE-FLOAT
• 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


NUMERIC DATA TYPE-COMPLEX

• 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
STRING DATA TYPE - STR
• Sometimes called as Character String
• String data types are sequences of characters that are enclosed in a single (‘ ‘)
or double quotation mark (“ “)
• For example to print some kind of message or an understandable instruction
such as displaying Hello World as your output.
BOOLEAN DATA TYPE
• At a certain part of the program, the decision is
required for a particular assigned condition, which
demands for a result.
• In programming, this result can only represented
by Boolean data type which either TRUE (1) or
FALSE(0).
INPUT STATEMENT
• Most of the programs require user to key-in the necessary input value so it could proceed for another
logical step in the sequence that was defined in flowchart.
• In Python, programmers use the input ( ) function to read input values from keyboard and store it
in a specific variables.
• By default, the data type of the variable will be string.
• If the variable used to store numeric value, then it must be further declare using appropriate data type
such as float or int.
Choosing right data type is important

For example:
Data type set by the programmer is int, therefore
If the user key in score as 5.5, error will be appear.

Thus, programmer should choose right data type


by changing int to float.
OUTPUT STATEMENT

• Once the input value has been processed, e.g after it underwent a series of arithmetic calculation, the
output result should be displayed on the computer screen.
• To display output, the programmer uses the print ( ) function, with argument in the bracket.
• We can improvise our program by creating more argument in the block of print ( ) function.
• To write this statement, a string element is needed as part of the argument together with the variable in
the block of ( ) function, as shown below:
..Continue

• If we want to separate each argument in print ( ) function, we use


keyword argument, sep=
• By default, print ( ) function only allocate a single space between
its printed items.
• The sep object is applied in the print ( ) function to insert the
string between each printed or displayed item.
FORMATING
OUTPUT
Ways to display the output of the program
Decimal Places
• Built-in function in Python, called round ( ), which take two numeric
argument.
• Round (x,y), x is a numeric value that is required to be rounded in a
decimal point of y value.
String Manipulation using . Format Object
• This is built-in function for values and variable substitution and for value formatting within the
string element.
• Programmers use { } in the string statement to mark where a variable will be substituted with
value(s) stated in .format argument.
EXPRESSION AND OPERATOR

• An expression is a particular concept where a number of variables, operators and


functions are put together in a single statement. It is constructed using
programming language called ‘Python interpreter.

• For example:
• a = b + c  This is arithmetic expression
• i > j  Boolean Expression
• mark > = 0 and marks <= 100  Compound Boolean expression
• An operator is define as a symbol that tells the programming interpreter in this
case Python language to perform mathematical or constructing Boolean expression
• There are 4 main categories of operators;
(1) Arithmetic Operator
Operators Name
+ Additional
- Subtraction
* Multiplication
/ Division
% Modulus

(2) Assignment Operator – use to assign value into variable

Operators Name
= Assignment
(3) Relational Operator – Text or defining relationship of two variables

Operators Name
== Equal to
> Greater than
< Less than
>== Greater than or equal to
<== Less than or equal to
!= Not equal to

(4) Logical Operator – Use to control program flow


Operators
and
or
not
ARITHMETIC EQUATION

This will be automatically assigned as float


MATHEMATICAL LIBRARY FUNCTION
• To perform complex operation, pre-defined mathematical function is stored in library.
• To use this function, we need to call or import the math library.

Function Description
sqrt (x) Return the square root of x
pow (x,y) Return value of x to the power of y
sin (x) Return the sine value of x
cos (x) Return the cos value of x
pi The mathematical constant pi (3.14159..)
degree (x) Converts angle x from radians to degrees
radians (x) Convers angle x from degrees to radians
Must call this import math library that
contain function

You might also like