0% found this document useful (0 votes)
23 views22 pages

Data Type

Uploaded by

hanyeelovesgod
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)
23 views22 pages

Data Type

Uploaded by

hanyeelovesgod
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/ 22

035.

001 Spring, 2024

Digital Computer Concept and Practice


Data Types

Soohyun Yang

College of Engineering
Department of Civil and Environmental Engineering
Data type 1 - Numbers
 Three types of numbers exist.
1) Integer : A whole number without a fractional part.
e.g.) 0, -153, 2*10**3
2) Float : Real numbers with a decimal point.
>Note 1: The scientific notation ‘e’ (i.e., the power of 10) yields always float-type.
e.g.) 97.4, -3.5e-1, 2e+3
3) Complex : A number with real and imaginary components.
>Note 2: The character ‘j’ is only valid for an imaginary component.
>Note 3: A number must be multiplied with the character ‘j’.
e.g.) 3+1j, -4.5-2j, 9+0.5j
Data type – Numbers (con’t)
 A number’s type can be checked  A number’s type, except complex,
via the type() function. can be converted to another.
• int(float)
• float(integer)
• complex(integer or float)
Data type 2 - String
 A string : A collection of characters that are set between
apostrophes (‘ ’), quotation marks (“ ”), or triple apostrophes or
quotation marks.
• Same results from the different four options!
• Hence, you can use one of the four options.
• .... BUT, you should be careful if your string
includes apostrophe or quotation mark.
• e.g.: how to make a string variable Dog’s cute ?
Data type – String (con’t)
 Operations
1) Concatenation : The plus symbol (+) is used to combine strings.
>Note : The str() function converts a type from number to string.
Otherwise…

2) Repetition : The multiplication operator (*) is used to repeat a string.


Data type – String (con’t)
 Operations (con’t)
3) Indexing : Provide a position to individual elements in a string.
>Note : Punctuation marks and spacing have an index. E.g.) The msg52 variable :
Elements W a t e r D a y i s M a r c h 2 2 .
Non-
negative
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Negative
Index
-22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

4) Slicing : Extract a part of a string via elements’ index.


• msg52[lower_limit_index X : upper_limit_index Y], X is inclusive but Y is exclusive.
• Leaving out X or Y goes all the way to the beginning or the end of a string.
Data type – String (con’t)
 Formatting characters
• Empty string “ ” : A string without any value in it.
• Backslash “ \ “ : Continue a code’s line on the next.
• \n : Put the characters followed by it into a new line.
• \t : Put a tab space.
>Note: One size of a string is added for \n and \t.
 Inserting a value in a string
• %s : string
• %d : integer
• %f : float
>Note: Format (2.546, ‘.1f’)
: rounding up a given number until
1st digit after a decimal point. -> string-type
Data type – String (con’t)
 Built-in functions
• Y.count(“X”) : Total number of a character X in a string Y
• Y.find(“X”) : Lowest index of X in Y (-1 if not exists)
• Y.index(“X”) : First index of X in Y (Error if not exists)

• Y.title() : Upper case for the first character in Y


• Y.upper() : Upper case for all characters in Y
• Y.lower() : Lower case for all characters in Y

• Y.replace(“A”, “B”) : Replace the word A with B in Y


Data type 3 - List
 A list : A collection of any kind of object (e.g., number, string, list).
• Squared brackets “[ ]” start and end a list.
• Comma “ , “ differentiate elements in a list.

 Objects in a list can be referred to, used, and changed.

Elements 13 4 2
Non-negative
Index
0 1 2
Negative
Index
-3 -2 -1
Data type – List (con’t)
 Concatenation (+) and repetition (*) work.

 Elements in a list do not have to be the same type. float


str
int
list
Data type – List (con’t)
 Built-in functions
• del L[k] : Delete the element with index k in a list L.
• L.append(X) : Append the object X to the end of L.
• L.pop() : Remove and return element at the last index
(0, for the first).

• L.count(X) : The number of X’s occurrences in L.


• L.index(X) : First index of X in L (Error if not exists).

• L.sort() : Sort the list L in ascending order


(reverse=True, for descending).
Data type 4 - Tuple
 A tuple : A collection of elements that are immutable but can be
used.
• Parentheses “( )” start and end a tuple.
• Comma “ , “ differentiate elements in a tuple.

Elements 1 3 5 7 Immutable
Non-negative
Index
0 1 2 3 in a direct
Negative
way!
Index
-4 -3 -2 -1
Data type – Tuple (con’t)
 Operations for the tuple are mostly similar with those for a list.
• Concatenation (+) and repetition (*) work.

 Elements in a tuple do not have to be the same type. float


str
int
list
tuple
Data type – Tuple (con’t)
 If you want to revise a pre-defined tuple:
• 1) Re-define the entire tuple.

• 2) Convert the tuple to a list  change the list  convert the list back into a tuple.
.list() function .tuple() function
Data type 5 - Dictionary
 A dictionary : An unordered collection of key:value pair elements.
=> Not stored as typed-order.
Thus, no numeric index exists.
>Note: Useful format to store lots of info (e.g., model parameters, plot setting)
• “Key:Value” indicates “Label:Contents”.
• Braces “{ }” start and end a dictionary.
• Comma “ , “ differentiate elements in a dictionary.

Key Value
name Minu Gu

id 170390

year 2

Not stored as typed-order!


Data type – Dictionary (con’t)
 A value is referred by keys, not by positional index.
 Existence of keys can be searched.
 A new key:value pair can be added.
• .update() for adding multiple pairs

 Existing key:value pair can be revised and deleted.


Data type – Dictionary (con’t)
 Individual keys and values, and their pairs can be returned.
• .keys()
• .values()
• .items()

 Keys and values can be converted into a list or a tuple. => Easy to manage.
• .list()
• .tuple()

 Dictionaries can be elements in a list or tuple.


 Lists and tuples can be nested in a dictionary.
Data type 6 - Boolean
 A boolean : A variable that is True or False.
>Note: Provide efficient ways to track the state of a program.
 Boolean variables can be defined via :
• Direct assignment Distinguish with
the string-type!
• Boolean expression with logical operators
Operator Syntax
Is equal to X == Y
Is not equal to X != Y
Greater than X>Y
Greater than and Equal to X >= Y
Lower than X<Y
Lower than and Equal to X <= Y
Data type – Boolean (con’t)
 Boolean variables can be used in arithmetic.
>Note: Logical quantity as 1 (True) and 0 (False)
0 * 1000

 Boolean algebra operators


=> Enable to describe complex boolean expressions.
Operator Purpose
and To see whether both the operand values are True.
or To see whether at least one of the value is True.
not To result the opposite boolean value coming after the not.

>Note: Operator precedence order “ Arithmetic operators > not > and > or ”
Data type – Boolean (con’t)
 Boolean algebra operators (con’t)

True and True

True or False

not True
Where does ‘Boolean variable’ come from?
 George Boole (1815, England – 1864, Ireland)
• Mathematician, Philosopher, and Logician
• The author of The Laws of Thought (1854),
which extended Aristotle’s logic and
attempted to give it a mathematical foundation.
=> named ‘Boolean Algebra’.
• A fundamental contributor to
the development of digital electronics
and all modern computer languages.

https://en.wikipedia.org/wiki/George_Boole
https://simplycoding.in/boolean-algebra/
https://www.maa.org/press/periodicals/mathematical-treasure-boole-s-laws-of-thought
Take-home points (THPs)
-
-
-
…

You might also like