Basic of Numeric Data Types, ICT Cource
Basic of Numeric Data Types, ICT Cource
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
Circumference of
Diameter the circle = 2𝝅𝒓
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
3. Mission
3.3. Programming plan
Pseudocode Flow chart
Start
[1] Start
[3] Take the radius value input Take radius value input
End
UNIT 03
3. Mission
3.4. Printing the circumference and area of the circle: final code
100 20
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
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
# 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
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
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.
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.
Focus The * operation of the string must be an integer. Otherwise, an error will occur.
TypeError
UNIT 03
TypeError
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
Line 1
• Calculate addition
Line 2
• Calculate subtraction
UNIT 03
Line 3
• Calculate multiplication
Line 4
• Floor Division(quotient): it returns 1, which is the quotient of dividing 5 by 3.
UNIT 03
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
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
Line 1 ~ 4
• Addition, subtraction, multiplication, and division of complex numbers
UNIT 03
Focus The data type is critical because the operator to be used varies depending on the data type.
집중
UNIT 03
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
Line 3
• When a number (real number) with decimal points is an input, it is marked as float.
UNIT 03
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
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
Line 1
• String cannot be subtracted, but it can be added. Two strings can be added together to connect.
UNIT 03
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
Line 1 ~ 2
• The strings "~~" and "~~" show the same output result.
UNIT 03
Line 1 ~ 2
• String and numerical values were printed using commas.
• The comma automatically adds a space when outputting.
UNIT 03
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
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
Line 1
• When entering a tap character in the middle of a sentence, enter \t at the corresponding location.
UNIT 03
Line 1
• When entering a tap character in the start of a sentence, enter \t as a first output character.
UNIT 03
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
Line 1
• Python supports negative indexing, and when indexing -1, the last character is indexed.
UNIT 03
Line 1
• Python supports negative indexing, and when indexing -5, the very first character is indexed.
UNIT 03
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