TalkFile_Chapter02. Strings, variables, input & output functions.pdf
TalkFile_Chapter02. Strings, variables, input & output functions.pdf
Value Where
should I save
it?
Container
* also means
30 20 10 repetition.
Create a variable
• To create a variable in Python, do the following:
>>> x = 100
>>>
100
x
Create a variable
• Any number of different values can be stored in the created variable.
>>> x = 100
>>> x = 200
>>> print(x) 200
200
x
100
x
Create 2 variables
• If you create a variable with a different name, a separate storage space is created as shown in
the figure.
>>> x = 100
>>> y = 200
100 200
x y
Calculation using variables
• If you create a variable with a different name, a separate storage space is created as shown in
the figure.
>>> x = 100
>>> y = 200
>>> sum = x + y
>>> print(sum)
300
300
100 200
sum = x + y
Caution!!
• One of the most common mistakes beginners make is interpreting = as ‘both sides are equal’.
• In Python, the = symbol means ‘store a value in a variable’.
• Let’s not get confused.
• The equal sign is expressed as ==.
100 200
sum = x + y
Challenge Problem
• What will be printed?
>>> x = 7
>>> y = 6
>>> print(x + y)
___________________________________________
>>> x = ‘7’
>>> y = ‘6’
print(x + y)
___________________________________________
challenge
The name of the variable
• Use meaningful names
• Lowercase and uppercase letters are treated differently.
• Variable names consist of English letters, numbers, and underscores (_).
• Variable names cannot contain spaces. Use underscores (_) to separate words.
index
sum
Identifier
sum # Starts with an English alphabet letter
_count # It can start with an underscore character.
number_of_pictures # You can put an underscore character in the middle.
King3 # You can also enter numbers, as long as it's not the very first one.
Camel Case
This is also possible!
score = 10
score = score + 1
11
10
score = score + 1
Print multiple values together
x = 100
y = 200
sum = x + y
print(“The sum of”, x, “and”, y, “is”, sum, “.”)
"
Enter the first input() int()
integer:
"
+ 700
"
Enter the
second integer: input() int()
"
Get string input from user
"Ainura"
name
Challenge Problem
• Let's write the following program that asks for the user's name, then takes two integers, adds
them, and prints the result.
challenge
Lab: Making a Robot Reporter
• Ask the user about the stadium, score, winning team, losing team, and best player and store
them in variables. Write an article by appending sentences to these strings.
===========================================
Today, a baseball game was held in Seoul.
Samsung and LG had a fierce battle.
Lee Kyoung-yong played a great game.
In the end, Samsung won against LG 8:7.
===========================================
What we learned in this chapter