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

Chapter 02

Variables and
input/output
functions
Table of contents

2.1 Data types and variables

2.2 Operation of variables

2.3 Input/output function


2.1 Data types and
variables

Big Data Analytics &


Artificial Intelligence
Starting with Python
Data type

▪ Data Type
– Refers to the types of various data values ​handled by
computers.
– Integer type(3), real number type(3.5), string type
▪ Data types in python
– Integer type (int)
– Real number (float)
– String type (str)
– Boolean (bool)
Integer
▪ Perform four arithmetic operations using
integers
The integer 3 and the integer 5 are added using the
addition operator.
– If you enter the addition expression 3 + 5 after the
prompt ‘>>>’ and press Enter, the value 8 is output under
the command.
– Python allows you to immediately check the output
results interactively after input.
String type
▪ String type (str)
– A data type for storing words or sentences
– There is no character type (char) in Python.
Variable
▪ Variable
– The name of a memory space where data can be stored
– A name given to a particular memory so that the
programmer can easily access the data.
– Content can be continuously updated with different
values

Term Function
Variable The name of memory space where data can
be stored
Data type Different types of data values handled by c
ompyters
Equal sign
▪ (=) equal sign
– Not an arithmetic calculation
– Operator to store the value on the right in the left

Operator Function
= Substitute the right value into the left
Integer and real variables
▪ Integer variable
① An integer variable named num is created in
memory.
② Store 3 in the integer variable num

▪ Real variable
String variable
▪ String variables in python
– Use single quotation marks (' ')
– Using character and string types together
– You can also use double quotes (" ")
Boolean variable
▪ boolean variable
– Indicates the data type as bool, an abbreviation for
boolean.
– False True or False
2.2 Operation of
variables

Big Data Analytics &


Artificial Intelligence
Starting with Python
Arithmetic operator

▪ Arithmetic operator
– (Type Conversion) The sum of integers and real
numbers is performed without special type
conversion.
Relational operator

▪ Relational operator
– Calculate the relationship between two
values ​or variables
– (False) The result is True or False
▪ Relational expression
– a > 0 : “a is greater than zero.”
– Output True if true otherwise False
– Interpret left side of operator as subject
Relational operator

▪ Training
Logical operator

▪ Logical operator
– Perform logical operations between two values ​or
variables
– The result is True or False

Operator Meaning
and And
or Or
not negative

Logical expression (a>0) and (a>b)


Meaning a is greater than 0 and a is greater than b
Logical operator
▪ and
– False If either operand is False, the result is also False.
▪ or
– True If either operand is True, the result is True
▪ not
– Convert to the opposite of operand value
Bit operator
Operator Meaning
▪ bit operator & Bitwise And

– perform operations bit by bit | Bitwise Or


^ Bitwise Xor
~ Bitwise Not
<< Bitwise Left shift
>> Bitwise Right shift

▪ bit operator logic


– Think of 1 as True and 0 as False
2.3 I/O function

Big Data Analytics &


Artificial Intelligence
Starting with Python
Output function

▪ print() function
– Transmits the object to an output device (screen) or a
text stream file
Integer type, real number type, boolean type data
value output

Integer

Float

Bool

▪ Print() function
– Formulas can be specified as inputs to functions
– Output after calculating the formula
Output what is stored in a variable

Formulas using variables can be calculated and the result


can be output at the same time
Outputting values of different
data types
Output multiple data types

– To output multiple values or values stored in


variables, connect each content with a comma (,).

– Values stored in variables and strings can be output


together
C language pseudomorphic output format

▪ A method similar to the printf() function used in C


language
Use the value or variable you want to output after
‘%’
Message Meaning
%s String
%c Character
%d Integer
%f Float
%o Octal number
%x Hexadecimal
%% % character
Input function - input string data
value
▪ Enter string data value
– use the input() function
– User must press Enter after entering string
– Need to print out instructions for users
Enter integer data value

▪ Enter integer data value


– When input is received with the input() function, both
numbers and strings are stored as strings.
– Use the int() function to convert a string into an integer
type for calculation
Enter real data value

▪ Enter real data value


– When input is received with the input() function, it is
stored as a string.
– Use the float() function to convert a string to a real
number.
Enter real data value

▪ int() function
– Real-valued data can also be converted to integers.
– Numbers after the decimal point cannot be stored
– Error in string format
Input boolean data value

▪ bool() function
– If a string type value is entered as the input value of the
bool() function, it becomes True unconditionally.
– Convert input value to integer type and then back to
boolean type

You might also like