0% found this document useful (0 votes)
3 views21 pages

TalkFile_Chapter02. Strings, variables, input & output functions.pdf

Chapter 2 covers the basics of strings, variables, and input/output functions in programming. It explains how to create and use variables in Python, including naming conventions and the importance of meaningful names. The chapter also includes examples of user input and challenges to reinforce learning.
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)
3 views21 pages

TalkFile_Chapter02. Strings, variables, input & output functions.pdf

Chapter 2 covers the basics of strings, variables, and input/output functions in programming. It explains how to create and use variables in Python, including naming conventions and the importance of meaningful names. The chapter also includes examples of user input and challenges to reinforce learning.
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/ 21

Chapter 2.

Strings, variables, input/output functions


Structure of a typical program
• A typical program receives data from outside (input stage), processes the data (processing
stage), and then displays the results on the screen (output stage).

Data Entry Data processing Data Output


Introduction to variables
• A variable can be thought of as a box that stores a value.
• Variables are created in the computer's memory space.

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 ==.

>>> x = 100 300


>>> y = 200
>>> sum = x + y
>>> print(sum)
300

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.

From now on I'll


call you index index index

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.

2nd_base (X) # Cannot start with a number.


money# (X) # Symbols such as # cannot be used.
Camel body
• Camel case is a method of writing in which the first letter of a variable is lowercase
and the first letter of the remaining words is uppercase. For example, in myNewCar,
the first 'm' is lowercase and the first letter of the remaining words is uppercase.

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, “.”)

The sum of 100 and 200 is 300.

100 200 300


x y sum

print(“The sum of”, x, “and”, y, “is”, sum, “.”)


Get integer input from user

How to use input()

Variable Converts a user-entered string into a number.

x = int(input(“Enter the first integer : ”))

Prints a guidance message and receives the value entered


by the user in string format.
Complete code

x = int(input("Enter the first integer: "))


y = int(input("Enter the second integer: "))
sum = x + y
print("The sum of", x, "and", y, "is", sum, ". ")

Enter the first integer: 300


Enter the second integer: 400
The sum of 100 and 200 is 300.
Complete code

"
Enter the first input() int()
integer:
"

+ 700

"
Enter the
second integer: input() int()
"
Get string input from user

name = input("Enter your name: ")


print(name, ", hello?")
print("Welcome to Python.")

Enter your name:


Ainura, hello?
Welcome to Python.

"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.

Enter your name: Ainura


Hello, Ainura.
Welcome to Python.
Enter your first integer: 300
Enter your second integer: 400
The sum of 300 and 400 is 700.

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.

Where is the stadium? Seoul


Which team won? Samsung
Which team lost? LG
Who is the best player? Lee Kyoung-yong
What is the score? 8:7

===========================================
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

• In a computer, you can use


variables to store something
in the computer's memory.
• Variables have names.
• Variables can store not only
numbers but also strings. In
fact, they can store anything.

You might also like