Practical Python for Beginners
DATA TYPES, INPUT, AND OUTPUT
Sarah Holderness
PLURALSIGHT AUTHOR
@dr_holderness
Where Do We Write Python Code?
The Python Interpreter A Python Script
script.py
>>> 10 + 20 + 30
60 amount = 10 + 20 + 30
print(total)
The Python interpreter A Python script is where
let’s you run Python lines you will create longer
of code one at time. Python programs.
The Python Interpreter
To start the
Python interpreter
on a Mac enter
python3 from the
Terminal.
To start the
Python interpreter
in Windows enter
py from the
Command Prompt.
Saving Numbers to Variables
Assigning the value 10 to
the variable length.
>>> length = 10
10 Now on your computer
there is a piece of
length memory labeled length
that stores the value 10.
Saving Numbers to Variables
>>> length = 10
10
>>> length
10
length
From the interpreter we can
enter the name of the variable
length to see its value and
see that it’s actually 10.
Saving Numbers to Variables
>>> length = 10
10 20
>>> width = 20
length width
Let’s also add the width of the
rectangle. Now we have another
variable stored in memory.
Saving Numbers to Variables
>>> length = 10
10 20 200
>>> width = 20
>>> area = length*width
length width area
Now we can calculate the area with And now we have another
the multiplication, * , operator. variable stored in memory.
The arithmetic operators in Python are mostly the same
ones you know already from a calculator: +, -, *, /
Saving Numbers to Variables
>>> length = 10
>>> width = 20 10 20 200
>>> area = length*width
length width area
>>> area
200
The value of area is
output to the screen.
Primitive Data Types
Python assumes the type of a variable based on the assigned value
int float
>>> amount = 10 >>> amount = 10.50
Python infers that Python infers that
amount is an int since amount is a float
it is a whole number since it is decimal
Saving an int and a float to Variables
>>> amount = 10
>>> tax = .06
Calculating the Total with Sales Tax
>>> amount = 10
>>> tax = .06
>>> total = amount + amount*tax
>>> total
10.6
A Python Script
A file containing code written in Python
sales_tax.py
amount = 10 > python3 sales_tax.py
tax = .06
total = amount + amount*tax
How to run a Python
script - python followed
by the filename.
(The command can also
be python3 on Mac or
py on Windows.)
A Python Script
sales_tax.py
amount = 10 > python3 sales_tax.py
tax = .06 How do we output
total = amount + amount*tax a value from a
total
Python script?
Notice there’s no output.
Just typing the value
we want doesn’t work
like in the shell.
A Python Script
sales_tax.py
amount = 10 > python3 sales_tax.py
tax = .06 10.6
total = amount + amount*tax
print(total)
Now the value of total
We can call the is printed to the screen
print() function
Python print() Function
print(total)
function argument
name
10.6 print() > 10.6
total
You can think of this function But we do know it
as a black box machine. will output the given
We don’t know how it works… value to the screen.
Data Type Conversion Functions
What if we want to convert a float to an int? Or vice versa?
int() float()
>>> amount = int(10.6) >>> amount = float(10)
>>> amount >>> amount
10 10.0
Use the int() Use the float()
conversion function conversion function
A String Stores Text
greeting.py
name = 'Sarah' Creating a String > python3 greeting.py
with single quotes Sarah
print(name)
The string
'Sarah' is saved The value of name
to the variable name prints without quotes.
The quotes are only
used tell Python that
anything inside them
is a String.
Create Strings with Single or Double Quotes
greeting.py
store_name = "Sarah's Store" Double quotes can be useful
print(store_name) if a single quote is literally
part of the String
store_name = 'Sarah's Store'
print(store_name)
This would cause an error because the second
single quote would end the String and Python
wouldn’t know what to do with the rest.
String Concatenation
greeting.py
hello = "Hello" > python3 greeting.py
name = "Sarah" HelloSarah
greeting = hello + name
print(greeting)
Notice how the Strings are
smushed together? We need
a space between them.
Concatenate two
Strings with a +
Fixing Our Program
greeting.py
hello = "Hello" > python3 greeting.py
name = "Sarah" Hello Sarah
greeting = hello + " " + name
print(greeting)
Concatenate
a space
Fixing Our Program
greeting.py
Let’s ask the user
hello = "Hello" for their name. > python3 greeting.py
name = "Sarah" Hello Sarah
greeting = hello + " " + name
print(greeting)
How can we customize
this program for other
names?
Python input() Function
>>> my_name = input("What’s your name?")
function The argument
name is a message
The message gets
The String the What’s your name? printed to the screen
user types in is
then saved to Sarah The program waits
the variable for the user to input
something and press
enter
Console Input
greeting.py
hello = "Hello" > python3 greeting.py
name = input("What’s your name?") What’s your name?Bob
greeting = hello + " " + name Hello Bob
print(greeting)
input() prints the statement, then Notice how the name
waits for a value from the console Bob is now printed
inside of the greeting.
Console Input
greeting.py
hello = "Hello" > python3 greeting.py
name = input("What’s your name?") What’s your name?Bob
greeting = hello + " " + name Hello Bob
print(greeting)
This looks bad. Can
we enter the name on
the next line?
Improving Our Program
greeting.py
hello = "Hello" > python3 greeting.py
name = input("What’s your name?\n") What’s your name?
greeting = hello + " " + name Bob
print(greeting) Hello Bob
/n is a special Now input is entered
character for a on the next line.
Summary of Primitive Data Types
int float
>>> amount = 10 >>> tax = .06
string
>>> name = "Sarah"
Summary of Input and Output
Input
>>> name = input("What’s your name?\n")
What’s your name?
Sarah
Output
>>> print("Hello " + name + "!!")
Hello Sarah!!