
Text book introduction and first chapter
Summary
…
#
each chapter of the book will be preceded by a summary of what the chapter is about and the chapter questions that you can have them in mind as you read through the chapter text and workout the code string transcription.
#
Copy and paste this entire post to your Google Docs document or Microsoft Word document and then use the space provided to transcribe the code strings.
#
You can compute expressions with a calculator or type string concatena-
tions with a word processor. You can even do string replication easily by
copying and pasting text. But expressions, and their component values—
operators, variables, and function calls—are the basic building blocks
that make programs. Once you know how to handle these elements, you
will be able to instruct Python to operate on large amounts of data for you.
It is good to remember the different types of operators (+, -, *, /, //, %,
and ** for math operations, and + and * for string operations) and the three
data types (integers, floating-point numbers, and strings) introduced in this
chapter.
A few different functions were introduced as well. The print() and input()
functions handle simple text output (to the screen) and input (from the key-
board). The len() function takes a string and evaluates to an int of the num-
ber of characters in the string. The str(), int(), and float() functions will
evaluate to the string, integer, or floating-point number form of the value
they are passed.
In the next chapter, you will learn how to tell Python to make intelli-
gent decisions about what code to run, what code to skip, and what code to
repeat based on the values it has. This is known as flow control, and it allows
you to write programs that make intelligent decisions.
#
…
#
Practice Questions
…
- Which of the following are operators, and which are values?
*
‘hello’
-88.8
–
/
+
5
…
…
- Which of the following is a variable, and which is a string?
spam
‘spam’
…
…
- Name three data types.
…
…
4. What is an expression made up of? What do all expressions do?
…
…
5. This chapter introduced assignment statements, like spam = 10. What is
the difference between an expression and a statement?
…
…
6. What does the variable bacon contain after the following code runs?
bacon = 20
bacon + 1
…
…
7. What should the following two expressions evaluate to?
‘spam’ + ‘spamspam’
‘spam’ * 3
…
…
8. Why is eggs a valid variable name while 100 is invalid?
…
…
9. What three functions can be used to get the integer, floating-point
number, or string version of a value?
…
…
10. Why does this expression cause an error? How can you fix it?
‘I have eaten ‘ + 99 + ‘ burritos.’
…
…
Extra credit: Search online for the Python documentation for the len()
function. It will be on a web page titled “Built-in Functions.” Skim the
list of other functions Python has, look up what the round() function
does, and experiment with it in the interactive shell.
#
…
What Is Programming?
…
#
“Do this; then do that.”
“If this condition is true, perform this action; otherwise, do that action.”
“Do this action that number of times.”
“Keep doing that until this condition is true.”
#
…
#
You can combine these building blocks to implement more intricate
decisions, too. For example, here are the programming instructions, called
the source code, for a simple program written in the Python programming
language.
Starting at the top, the Python software runs each line of code
(some lines are run only if a certain condition is true or else Python runs
some other line) until it reaches the bottom.
u
passwordFile = open(‘SecretPasswordFile.txt’)
v secretPassword = passwordFile.read()
w print(‘Enter your password.’)
typedPassword = input()
x if typedPassword == secretPassword:
y print(‘Access granted’)
z if typedPassword == ‘12345’:
{ print(‘That password is one that an idiot puts on their luggage.’)
else:
| print(‘Access denied’)
You might not know anything about programming, but you could prob-
ably make a reasonable guess at what the previous code does just by reading
it. First, the file SecretPasswordFile.txt is opened u,
and the secret password in
it is read v.
Then, the user is prompted to input a password (from the key-
board) w.
These two passwords are compared x,
and if they’re the same,
the program prints Access granted to the screen y.
Next, the program checks
to see whether the password is 12345 z and hints that this choice might not
be the best for a password {. If the passwords are not the same, the pro-
gram prints Access denied to the screen |.
#
…
NOTE – #u#… Letters surounded by # # are place markers
Not code… do not transcribe
…
#u# passwordFile = open(‘SecretPasswordFile.txt’)
#v# secretPassword = passwordFile.read()
#w# print(‘Enter your password.’)
typedPassword = input()
#x# if typedPassword == secretPassword:
#y# print(‘Access granted’)
#z# if typedPassword == ‘12345’:{ print(‘That password is one that an idiot puts on. . their luggage.’)
else:
print(‘Access denied’)
…
…
…
What Is Python?
…
#
See text book introduction chapter automate the boring stuff
#
…
Programmers Don’t Need to Know Much Math
…
#
See text book introduction chapter automate the boring stuff
#
…
Downloading and Installing Python
…
#
See text book introduction chapter automate the boring stuff
#
…
The Interactive Shell
…
#
Transcribe this code below into.(Qpython3)
interactive shel
…
>>> print(‘Hello world!’)
…
…
…
CHAPTER 1
Python Basics
…
Entering Expressions into the Interactive Shell
…
>>> 2 + 2
…
…
…
>>> 2
…
…
…
OPERATORS……………………………result
…
** Exponent 2 ** 3. …………… …………….8
% Modulus/remainder 22 % 8. …………….. 6
// Integer division/floored quotient 22 // 8….. 2
/ Division 22 / 8 ……………………………..2.75
* Multiplication 3 * 5 ………………………….15
– Subtraction 5 – 2 ……………………………..3
+ Addition 2 + 2……………………………….. 4
…
–TRANSCRIBE EACH 4 TIMES–
…
>>> 2 + 3 * 6
…
…
…
>>> (2 + 3) * 6
…
…
…
>>> 48565878 * 578453
…
…
…
>>> 2 ** 8
…
…
…
>>> 23 / 7
…
…
…
>>> 23 // 7
…
…
…
>>> 23 % 7
…
…
…
>>> 2 + 2
…
…
…
>>> (5 – 1) * ((7 + 1) / (3 – 1))
…
…
…
The Integer, Floating-Point, and String Data Types
…
#
Table 1-2:
Common Data Types
Data type Examples
Integers -2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers -1.25, -1.0, –0.5, 0.0, 0.5, 1.0, 1.25
Strings ‘a’, ‘aa’, ‘aaa’, ‘Hello!’, ’11 cats’
#
…
…
String Concatenation and Replication
…
–Transcribe code below–
…
>>> ‘Alice’ + ‘Bob’
…
…
…
>>> ‘Alice’ * 5
…
…
…
Storing Values in Variables
…
Assignment Statements
#
— See textbook explanation chapter 1 automatetheboringstuff
#
…
TRANSCRIBE BELOW
…
>>> spam = 40
>>> spam
…
…
…
>>> eggs = 2
>>> spam + eggs
…
…
…
>>> spam + eggs + spam
…
…
…
>>> spam = spam + 2
>>> spam
…
…
….
>>> spam = ‘Hello’
>>> spam
…
…
…
>>> spam = ‘Goodbye’
>>> spam
…
…
…
Variable Names
…
#
— see text book description chapter 1 automate the boring stuff
#
…
Your First Program
…
#
Transcribe each piece of code three times after you’ve done all of the code three times into it one time into the queue Python 3 shell and get it to work…
#
…
#
# I’m a comment because I’m preceded by a hash mark
#
# This program says hello and asks for my name. I’m also a comment
>>>print(‘Hello world!’)
…
…
…
>>>print(‘What is your name?’) # ask for their name
>>>myName = input()
…
…
…
>>>print(‘It is good to meet you, ‘ + myName)
>>>print(‘The length of your name is:’)
>>>print(len(myName))
…
…
…
>>>print(‘What is your age?’) # ask for their age
>>>myAge = input()
>>>print(‘You will be ‘ + str(int(myAge) + 1) + ‘ in a year.’)
…
…
…
# This program says hello and asks for my name.
>>>print(‘Hello world!’)
>>print(‘What is your name?’) # ask for their name
>>>myName = input()
>>>print(‘It is good to meet you, ‘ + myName)
>>>print(‘The length of your name is:’)
>>>print(len(myName))
>>>print(‘What is your age?’) # ask for their age
>>>myAge = input()
>>>print(‘You will be ‘ + str(int(myAge) + 1) + ‘ in a year.’)
…
…
…
Dissecting Your Program
…
#
See textbook for detailed descriptions and instructions chapter 1
#
…
The print() Function
…
#
The print() function displays the string value inside the parentheses on the
screen.
#
…
>>>print(‘Hello world!’)
>>>print(‘What is your name?’) # ask for their name
…
…
…
The input() Function
…
#
The input() function waits for the user to type some text on the keyboard
and press enter.
#
…
>>>myName = input()
…
…
…
Printing the User’s Name
…
#
The following call to print() actually contains the expression ‘It is good to
meet you, ‘ + myName between the parentheses.
>>>print(‘It is good to meet you, ‘ + myName)
…
…
…
The len() Function
…
print(‘The length of your name is:’)
print(len(myName))
…
…
…
>>> len(‘hello’)
…
…
…
>>> len(‘My very energetic monster just scarfed nachos.’)
…
…
…
>>> len(”)
…
…
…
The str(), int(), and float() Functions
…
>>> str(29)
…
…
…
>>> print(‘I am ‘ + str(29) + ‘ years old.’)
…
…
…
>>> str(0)
…
…
…
>>> str(-3.14)
…
…
…
>>> int(’42’)
…
…
…
>>> int(‘-99’)
…
…
…
>>> int(1.25)
…
…
…
>>> int(1.99)
…
…
…
>>> float(‘3.14’)
…
…
…
>>> float(10)
…
…
…
>>> spam = input()
101
>>> spam
‘101’
…
…
…
>>> spam = int(spam)
>>> spam
101
…
…
…
>>> spam * 10 / 5
202.0
…
…
…
>>> int(7.7)
7
>>> int(7.7) + 1
8
…
…
…
print(‘What is your age?’) # ask for their age
myAge = input()
…
…
…
print(‘You will be ‘ + str(int(myAge) + 1) + ‘ in a year.’)
…
…
…
#
The myAge variable contains the value returned from input()
#
…
#
Text and Number Equivalence
Although the string value of a number is considered a completely different
value from the integer or floating-point version, an integer can be equal to a
floating point.
>>> 42 == ’42’
False
>>> 42 == 42.0
True
>>> 42.0 == 0042.000
True
Python makes this distinction because strings are text, while integers and
floats are both numbers.
#
…
Comment (3)