Alpro Class 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

ALGORITHM AND

PROGRAMMING
Session 3

Anita Safitri, M.Kom.


• Identifier

Today’s Topic • Variable


• Data Type
• Operator

Anita Safitri, M.Kom.


Identifier
• Every object in program must be named or identified to differentiate it from other
objects in the program.
• In programming, objects can be labels, constants, variables, functions and procedures,
etc.
• The process of naming or identifying objects is called declaration
• Some things to consider when writing an identifier are:
✓ Starting with a letter or underscore
✓ Does not contain blanks
✓ Does not contain special symbols (e.g.: !, @, #, $, %, … etc)
✓ Not a reserved words in Pyhon.
✓ Case-sensitive.

Anita Safitri, M.Kom.


Identifier (1)
Standard Words in Python
False Class Finally Is Return
None Continue For Lambda Try
True Def From Nonlocal While
and Del Global Not With
as Elif If Ot Yield
assert else Import Pass
break Except in raise

Anita Safitri, M.Kom.


Identifier (2)
• Number
• _number
• Number bilangan = WRONG
• Number_bilangan
• NumberBilangan
• numberBilangan
• 34bilangan = WRONG

Anita Safitri, M.Kom.


Expression, Operator, Eperand
Operator

Variable

x=5+7:4 Expression

Operand
Anita Safitri, M.Kom
Variable
• Variables are containers for storing data values.
• Python has no command for declaring a variable. A variable is created the
moment you first assign a value to it.
• Variable names are case-sensitive.
• E.g.: name = ‘John’
age = 10
print (name)
print (age)

Anita Safitri, M.Kom.


Variable (1)
• Variables do not need to be declared with any particular type, and can even
change type after they have been set.
E.g.: age = 10 : is of type int
age = ‘ten’ : is now of type str
• If you want to specify the data type of a variable, this can be done with casting.
E.g.: age = str(10) : age will be ’10’
age = int(10) : age will be 10
age = float(10) : age will be 10.0

Anita Safitri, M.Kom.


Each variable has its own data type

Anita Safitri, M.Kom.


Data Type
• In Python programming, there are standard data types that can be grouped into:

BOOLEAN NUMERIC STRING LIST TUPLE DICTIONARY

Anita Safitri, M.Kom


Data Type (1)
• Boolean
✓ Boolean represent one of two values : True or False
✓ In programming you often need to know if an expression is True or False.
✓ You can evaluate any expression in Python, and get one of two answers, True or False.
✓ When you compare two values, the expression is evaluated and Python returns the
Boolean answer:
e.g.: print (5<1) result : False
print (3>1) result: True

Anita Safitri, M.Kom


Data Type (2)
✓ Boolean
✓ create a boolean value from a non boolean form can be done by using the bool()
function.
e.g.: tes = True
bool(tes) result : True
tes = False
bool(tes) result : False

✓ Any string is True, except empty strings.


✓ Any number is True, except 0.
✓ Any list, tuple, set, and dictionary are True, except empty ones.

Anita Safitri, M.Kom


Data Type (3)
• Integer.
✓ integer type or class int is the primary numeric type in python
✓ this type only recognizes integers (... -3, -2, 0, 1, 2, 3, ...)
✓ On python 3.x. the length of the integer is not limited.
✓ conversion of data into integer form can be done by using the int() function.

• Float
✓ This type only recognizes real numbers (a combination of fractions and integers)
✓ conversion of data into float form can be done by using the float() function.

Anita Safitri, M.Kom


Data Type(4)
• String
• This data type stores a string of characters
• Strings in python are surrounded by either single quotation marks, or double quotation
marks.
E.g.: 'hello' is the same as "hello".
name = ‘Salsabila’
postcode = “899002”

• Conversion of data into string class can be done by using the str() function.

Anita Safitri, M.Kom


Data Type(5)
• String
• To get the length of a string, use the len() function
E.g.: 'hello' is the same as "hello“

• To check if a certain phrase or character is present or not in a string, we can use the
keyword in and not in.
E.g.: txt = "The best things in life are free!”
print("free" in txt)

Anita Safitri, M.Kom


Data Type(6)
• List
• The most basic data structure in python is a sequence.
• This sequence contains elements. Each element has its own index number. Index
numbering starts from 0.
• The items in the list can consist of different data types

Ilustration of List
Elemen a b c 162 57 60112 x z
Index 0 1 2 3 4 5 6 7

• E.g.: list = [‘a’,‘b’, ‘c’, 162, 57, ‘60112’, ‘x’, ‘z’]

Anita Safitri, M.Kom


Data Type(7)
• Tuple
✓ tuples are immutable data types in python programming language.
✓ Tuple is a sequence, in which the contents of the tuple are immutable or read-only.
✓ tuples do not use square brackets like in lists, but instead use brackets.
✓ E.g.: number = (4,5,6,9,10)

• Dictionary
✓ an unordered collection of items. Like lists, dictionaries have indexes and the content of
elements is mutable.

Anita Safitri, M.Kom


Operators
• Operator is special symbols used to perform operations on variables and value
both arithmetic and logical operation.
• Python divides the operators in the following groups:
✓ Arithmetic operators
✓ Assignment operators
✓ Comparison operators
✓ Logical operators
✓ Identity operators
✓ Membership operators
✓ Bitwise operators

Anita Safitri, M.Kom


Operators - Arithmatic
• Arithmetic operators are used with numeric values to perform common
mathematical operations:

Anita Safitri, M.Kom


Operators - Assignment
• Assignment operators are used to assign values to variables:

Anita Safitri, M.Kom


Operators - Comparison
• Comparison operators are used to compare two values:

Anita Safitri, M.Kom


Operators – Logical
• Logical operators are used to combine conditional statements

Anita Safitri, M.Kom


Operators – Identity
• Identity operators are used to compare the objects, not if they are equal, but if
they are actually the same object, with the same memory location:

Anita Safitri, M.Kom


Operators – Membership
• Membership operators are used to test if a sequence is presented in an object:

Anita Safitri, M.Kom


Operators – Bitwise
• Bitwise operators are used to compare (binary) numbers:

Anita Safitri, M.Kom


EXERCISE!

Anita Safitri, M.Kom


THANKYOU

Anita Safitri, M.Kom.

You might also like