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

Basic of Numeric Data Types, ICT Cource

Basic of Numeric Data Types

Uploaded by

amirkhanovcr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Basic of Numeric Data Types, ICT Cource

Basic of Numeric Data Types

Uploaded by

amirkhanovcr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Unit learning objective (1/3) UNIT 03

Unit learning objective (2/3) UNIT 03

Learning overview

 Perform math operations including arithmetic operation, power, etc. using numeric data types.
 Find out data types of objects and learn how operators work depending on data types.
 Make an output using string data types.
Unit learning objective (3/3) UNIT 03

Numeric Data Arithmetic


Data Types
Types Operations

Strings String Indexing


UNIT 03

1. Real world problem


1.1. Observing the Shadow of a Black Hole
UNIT 03

1. Real world problem


1.2. Circumference and Area Formula of the Circle

Circumference of
Diameter the circle = 2𝝅𝒓

Radius (𝑟) Area of the circle


= 𝝅𝒓𝟐
UNIT 03

2. Solution
2.1. Python contributed greatly to the shadow observation of black holes.
UNIT 03

2. Solution
2.2. Python to solve the global climate crisis
UNIT 03

3. Mission
3.1. Print the circumference and area of the circle
In this mission, get an input of circle’s radius.
Calculate circumference and area of the circle using operators we will learn in this unit.
Print calculated circumference and area of the circle on the screen.
Put various numbers like 4, 5, 6, 10, 20, 33 for the radius.
UNIT 03

3. Mission
3.2. How printing the circumference and area of the circle works

(‘Enter the radius :’))

(‘Radius of the circle’, radius)


(‘Area of the circle’, circum)
(‘Circumference of the circle’, area)

Enter the radius : 4.0


Radius of the circle 4.0
Area of the circle 50.24
Circumference of the circle 25.12
UNIT 03

3. Mission
3.3. Programming plan
Pseudocode Flow chart

Start
[1] Start

[2] Define value of PI as 3.14 Define PI = 3.14

[3] Take the radius value input Take radius value input

[4] Calculate circumference using the radius input value


Calculate circumference

[5] Calculate area using the radius input value


Calculate area
[6] Print the circumference and area on the screen
Print the circumference
[7] End and area.

End
UNIT 03

3. Mission
3.4. Printing the circumference and area of the circle: final code

(‘Enter the radius :’))

(‘Radius of the circle’, radius)


(‘Area of the circle’, circum)
(‘Circumference of the circle’, area)
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.1. Computation using numerical information
In programming, operations using numerical information and then using the data frequently occur.
For example, information such as a person's height and weight is expressed as numerical values such as 177cm
and 58kg.

Ex Addition operations of the numbers 100 and 20


can be performed by 100 + 20.
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.1. Computation using numerical information
Let’s learn the terms for arithmetic operation. Data values or variables subject to the operation are called operands.
The subject of the operation is called the operator. Take an example of an operation of 100 + 200.

100 20

Operand Operator Operand


UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.2. Operators
The table below explains the operator of Python and the operation of the operator.

Operator Meaning Action


+ Addition Add the left operand and the right operand.
- Subtraction Subtract the right operand from the left operand.
* Multiplication Multiply the right operand from the left operand.
Divide the left operand into the right operand. Python's division basically returns
/ Division
the real number value.
Floor Unlike /, the result of division is used to discard the decimal point or less and obtain
//
Division(quotient) only the integer part.
It is read as a modulo operator and has nothing to do with the percentage that
% Remainder
means ratio. Finds the remainder of the division.
Multiply the left operand to the right operand. In the case of **0.5, a square root
** Power
operation is performed.
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.3. Example code using operator
Perform an operation using numerical data types. The operation may be performed using numerical data and operator
+, -, *, /, //, %.

# Find addition of numerical value of 100 and 20

# Find multiplication of numerical value of 100 and 20

# Find subtraction of numerical value of 100 and 20

# Divide numerical value of 100 with 20

# Divide numerical value of 100 with 20 and find quotient

# Divide numerical value of 100 with 20 and find quotient


UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.1. Classification of data types
In Python, the distinction of data types is very important.
Characters and numbers have the same form, but their properties can be very different.
Ex The number 10 can be calculated. However, the letter "10" is impossible to calculate.

Depending on the data type, the available operators are different. In the following case, a grammar error occurs
because the string "10" is divided by 2.

TypeError

String 10 divided by number 2

# String 10 divided by number 2


UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.1. Classification of data types
In Python, the operator applies differently depending on the data type.
The result of the multiplication (*) operation of the numbers 100 and 2 is 200.
However, the result of the multiplication (*) operation of the letters '100' and 2 is '100100'. In other words, the result of
the multiplication (*) operation of the letter ‘Hello’ and 2 is 'HelloHello', but the multiplication operation on the letter is
different from the multiplication operation on the numerical value.

# Find multiplication of numerical value 100 and 2

# Find multiplication of string ‘Hello’ and 2

# Find multiplication of string ‘100’ and 2


UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.2. Basic data types in Python
The basic data types and examples of Python, which are widely used, are as follows.

Data Types Example

Integers(int) …, -3, -2, -1, 0, 1, 2, 3, ...

Real Number(float) 3.14, 4.28, 0.01, 123.432

String(str) 'Hello World', '123', "Hi"

Bool(bool) True, False


UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.2. Basic data types in Python
Here are the important data structure of Python and types of variables. These will be used in coding.

Number Boolean String List Tuple Dictionary

int ex : ex : ‘hello’ ex : ex : ex :
True, False ‘100’ [10, 20, 30] (10, 20, 30) { ‘name’ : ‘David’,
ex : 10, 200 ‘age’ : 23,
‘height’ : 56.5 }

float
ex : 3.14

complex
ex : 4 + 3j
UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.3. How to distinguish data types
The terms referring to numerical values, functions, and classes, which are components of the Python program, is
object.
The role of the operator differs depending on whether the object's data type is integer or string.
Type function returns the type of an object. Let's check the data type using the code below.

# Let you know data type of 100

# Let you know data type of 100 Numeric data type of 100 and 100.0 are different
from each other.

# Let you know data type of 100 Write complex number in form of x + y j.

# Complex number data type in the form of real + imaginary numbers like 10 + 3j
UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.4. Precedence of operators
If the addition and multiplication operators appear in one sentence, the multiplication operation is performed first.
When parentheses appear in a sentence, the operators in parentheses are treated first.
The calculation results in the sentence vary according to the operator processing order.

# Multiplication is calculated first.

# Operator in parentheses is calculated first.


UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.5. Operator precedence chart
Let’s look at Python operators and precedence. These are the contents to be learned in this lecture.

Operator Explanation
() parenthesis operator
** power operator
~,+,- monadic operator
* , / , % , // multiplication, division, remainder operator
+,- addition, subtraction
>> , << bitwise movement operator
The higher the table,
the higher the priority. & bitwise AND operator
^,| bitwise XOR operator, bit OR operator
<= , < , > , >= comparison operator
== , != equal operator
= , %= , /= , //= , -= , += , *= , **= assignment operator, complex assignment operator
is , is not identity operator
in , not in membership test operator
not , or , and logical operator
UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.6. Data type conversion
Python data can be calculated by converting its data type. The string '10' can be int-type converted to int('10').
Therefore, int('10')/2 is executed without error.
In order to change the number 10 from the Python data to the string '10', str (10) is used. This converted string can be
written with the string as an addition operation.

# Convert string ‘10’ into integer and divide by 2.

# Convert number 10 into string and use + operator


UNIT 03
UNIT 03

3. String Application
3.1. Take a look at string
In Python, the string str is very suitable for displaying information in software.
All srt files can be converted to texts. However, as it is a text, it is impossible to apply arithmetic calculation.

Ex The user's name, ID, password, etc. may be expressed as a string.

Python's string is a form in which each letter is connected.


Addition and multiplication operations are possible for Python's strings.

String

T h i s i s a s t r i n g !

Character
UNIT 03

3. String Application
3.2. String operator +, *
Let's look at the operations + and * available in the string. The + operation of the string connects the two strings.
The * operation of the string must be an integer. That is, the string is generated by repeating the integer n.

# String can be added to strings.

# Generates string of ‘Python’ repeated 10 times

#Generates string of ‘*’ repeated 50 times

#String ‘100’ repeats 10 times

Focus The * operation of the string must be an integer. Otherwise, an error will occur.

TypeError
UNIT 03

TypeError

Addition of number and string causes an error.

"10" looks like a number, but since it is a character in the quotation m


ark, an error such as the above occurs.
Paper coding
Try to fully understand the basic concept before moving on to the next step.
Lack of understanding basic concepts will increase your burden in learning this
course, which may make you fail the course.
It may be difficult now, but for successful completion of this course we suggest you
to fully understand the concept and move on to the next step.
UNIT 03

Condition for
Execution

Time

Write the entire code and the expected output results in the note.
UNIT 03

Condition for
Execution

Time

Write the entire code and the expected output results in the note.
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.1. Arithmetic operation
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.1. Arithmetic operation
Find out arithmetic calculation through coding.

Line 1
• Calculate addition

Line 2
• Calculate subtraction
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.1. Arithmetic operation

Line 3
• Calculate multiplication
Line 4
• Floor Division(quotient): it returns 1, which is the quotient of dividing 5 by 3.
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.2. Power, remainder

Line 1 ~ 2
• 1: Power, 210 is performed to return 1024.
• 2: It is called modulo. It returns the remainder of 9 divided by 5 which is 4.
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.3. Mixed operation

Line 1 ~ 2
• 1: Number operations have the same precedence as general arithmetic operations. In other words,
multiplication and division are performed before addition.
• 2: If there is a parenthesis, it is calculated first.
UNIT 03

1. Numeric Data Types, Arithmetic and Division


1.4. Complex number operation
# Addition of complex numbers

2 # Subtraction of complex numbers

3 # Multiplication of complex numbers

4 # Division of complex numbers

Line 1 ~ 4
• Addition, subtraction, multiplication, and division of complex numbers
UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.1. Identifying and converting data types
Identifying and converting a certain value of data type is a very important issue.

int(“10”) # Convert string “10” to integer 10

Focus The data type is critical because the operator to be used varies depending on the data type.
집중
UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.1. Identifying and converting data types
Find out data types using type built-in functions

Line 1
• When a number (integer) is an input, it is marked as int.
Line 2
• When a string is an input, it is marked as str(string).
UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.1. Identifying and converting data types
Find out data types using type built-in functions

Line 3
• When a number (real number) with decimal points is an input, it is marked as float.
UNIT 03

# Cell output is int

# Cell output is <class ‘int’>


UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.2. How to convert a data type

Line 1 ~ 2
• 1: Use the int function to replace letters with numbers.
• 2: Since the string "50" has been changed to an integer by int ("50"), +20 operation is performed.
UNIT 03

2. Object Data Types, Operator Priorities, and Data Type Conversion


2.2. How to convert a data type

Line 3 ~ 4
• 3: To replace numbers with letters, use the str function.
• 4: Use the float function to replace the letter with a real number.
UNIT 03

3. Strings and Operators


3.1. Addition and Multiplication Operators of String
The string may use operators such as addition and multiplication. Subtraction and division operators cannot be used.

Line 1
• String cannot be subtracted, but it can be added. Two strings can be added together to connect.
UNIT 03

3. Strings and Operators


3.1. Addition and Multiplication Operators of String
Find out the addition and multiplication operators of strings.

Line 2
• When a string is multiplied by an integer, the string repeats. Here, 'repeat' is repeated 10 times.
• However, division and subtraction do not work.
UNIT 03

3. Strings and Operators


3.2. Single and Double Quotation Marks
When printing a string, the same result is obtained whether using single quotes such as 'Hello Python!!' or double
quotes such as “Hello Python!!”.

Line 1 ~ 2
• The strings "~~" and "~~" show the same output result.
UNIT 03

3. Strings and Operators


3.3. Using comma
When a string is to be an output, string and numerical value is printed using a comma.

Line 1 ~ 2
• String and numerical values were printed using commas.
• The comma automatically adds a space when outputting.
UNIT 03

3. Strings and Operators


3.4. Escape sequence
Escape sequence: \" or \" may be used to output double or small quotes on the screen.

Line 1 ~ 2
• String should be enclosed in double or double quotes.
• Therefore, it is difficult to indicate quotation marks. In this case, you can use \" or \".
• An escape sequence refers to a special character used to control a computer, and since a double quote or a
single quote is defined for defining a string, it is output using the control character \' or \' to output it.
UNIT 03

3. Strings and Operators


3.4. Escape sequence
Escape sequence: \n is used to change lines.

Line 1
• <Inverse Slash+Character> is used to display special characters such as line change. \n is a line-switching
character, and \t is a tap character.
UNIT 03

3. Strings and Operators


3.4. Escape sequence
Escape sequence: \t is used when a tap character is to be input.

Line 1
• When entering a tap character in the middle of a sentence, enter \t at the corresponding location.
UNIT 03

3. Strings and Operators


3.4. Escape sequence
Escape sequence: \t is used when a tap character is to be input.

Line 1
• When entering a tap character in the start of a sentence, enter \t as a first output character.
UNIT 03

3. Strings and Operators


3.5. String indexing
String indexing: String indexing may be used to obtain only some of the long strings. Use large brackets [] and
numbers.

Line 1
• String indexing starts from 0.
• That is, when zero is indexed, h, which is the first letter of "hello", is indexed.
Line 2
• When 2 is used as an index in string indexing, you can see that the third letter l is indexed.
UNIT 03

3. Strings and Operators


3.6. Negative string indexing
String indexing: String indexing may be used to obtain only some of the long strings. Use large brackets [] and
numbers.

Line 1
• Python supports negative indexing, and when indexing -1, the last character is indexed.
UNIT 03

3. Strings and Operators


3.6. Negative string indexing
String indexing: String indexing may be used to obtain only some of the long strings. It will be covered in detail later.

Line 1
• Python supports negative indexing, and when indexing -5, the very first character is indexed.
UNIT 03

3. Strings and Operators


3.6. Negative string indexing
Pay attention to the range of indexing.

Line 1
• The string, 'hello’ has 5 characters. Therefore indexing range is from 0 to 4.
caution: index that is out of the range prints error.
UNIT 03
UNIT 03
UNIT 03

Output Example

You might also like