
LESSON 1: PYTHON-FLOWCHARTS, INPUTS, AND VARIABLES
Presentation
•
Computers
•
7th Grade
•
Practice Problem
•
Hard
Esperanza Sabangan
FREE Resource
102 Slides • 0 Questions
1
2
Example text
Go ahead and replace it with
your own text. This is an
example text.
PYTHON TO GO: Synthesis & Evaluation
Learning Objectives
PYTHON SCENARIO: What is Python?
PYTHON SCENARIO: What is Python?
PYTHON CORNER: What do you think?
PYTHON EXPLORE: Algorithms & Flowcharts
3
What is Python?
Python
is
a
general
purpose
programming
language
that
is
often
applied
in
scripting
roles.
So,
Python
is
programming
language
as
well
as
scripting
language.
Python
is
also
called
as
Interpreted language
4
Differences between program
and scripting language
Program
Scripting
a
program
is
executed
(i.e.
the
source
is
first
compiled,
and
the
result
of
that
compilation
is
expected)
A
"program"
in
general,
is
a
sequence
of
instructions written
so
that
a
computer
can
perform
certain
task.
a script is interpreted
A
"script"
is
code
written
in
a scripting language. A
scripting
language
is
nothing
but
a
type
of
programming language in
which we can write code
to
control
another
software
application.
5
6
7
8
History of Python
BACK DROP
Source: Wikipedia
Invented
in
the
Netherlands,
early
90s by Guido van Rossum
Python
was
conceived
in
the
late
1980s
and
its
implementation
was
started in December 1989
Guido
Van
Rossum
is
fan
of
‘Monty
Python’s
Flying
Circus’,
this
is
a
famous TV show in Netherlands
Named after Monty Python
Open sourced from the beginning
9
Python’s Benevolent Dictator For Life
“Python
is
an
experiment
in
how
much
freedom
programmers
need.
Too
much
freedom
and
nobody
can
read
another's
code;
too
little
and
expressiveness
is endangered.”
- Guido van Rossum
10
Why was python created?
"My original motivation for creating Python
was the perceived need for a higher level
language in the Amoeba [Operating Systems]
project.
I
realized
that
the
development
of
system
administration
utilities
in
C
was
taking
too
long.
Moreover,
doing
these
things
in
the
Bourne
shell
wouldn't
work
for a variety of reasons. ...
So,
there
was
a
need
for
a
language
that
would
“bridge
the
gap
between
C
and
the shell”
- Guido Van Rossum
11
Example text
Go ahead and replace it with
your own text. This is an
example text.
PYTHON CORNER: What do you think?
PYTHON EXPLORE: Algorithms & Flowcharts
PYTHON TO GO: Synthesis & Evaluation
Learning Objectives
PYTHON SCENARIO: What is Python?
PYTHON CORNER: What do you think?
12
What can I do with Python?
System programming
Graphical User Interface
Programming
Internet Scripting
Component Integration
Database Programming
Gaming, Images, XML , Robot
and more
PYTHON CORNER
13
Who uses python today…
Python
is
being
applied
in
real
revenue-
generating products by real companies. For
instance:
Google makes extensive use of Python in its
web
search
system,
and
employs
Python’s
creator.
Intel,
Cisco,
Hewlett-Packard,
Seagate,
Qualcomm,
and
IBM
use
Python
for
hardware
testing.
ESRI
uses
Python
as
an
end-user
customization
tool
for
its
popular
GIS
mapping products.
The
YouTube
video
sharing
service
is
largely written in Python
14
Why do people use Python?
The following primary factors cited by
Python Users seem to be these:
Python is object-oriented
Structure supports such concepts as
polymorphism, operation overloading, and
multiple inheritance.
Indentation
Indentation
is
one
of
the
greatest
future
in
Python.
It's free (open source)
Downloading and installing Python is free and
easy and the source code is easily accessible
15
It's powerful
-
Dynamic typing
-
Built-in types and tools
-
Library utilities
-
Third party utilities (e.g. Numeric,
NumPy, SciPy)
-
Automatic memory management
It's portable
-Python
runs
virtually
every
major
platform
used
today
-
As
long
as
you
have
a
compatible
Python
interpreter installed, Python programs will run in
exactly the same manner, irrespective of platform.
Why do people use Python?
16
It's mixable
- Python can be linked to components written in other
languages easily
-
Linking
to
fast,
compiled
code
is
useful
to
computationally intensive
problems
- Python/C integration is quite common
It's easy to use
-
No intermediate compile and link steps as in C/
C++
- Python programs are compiled automatically to an
intermediate
form
called
bytecode,
which
the
interpreter then reads
-
This
gives
Python
the
development
speed
of
an
interpreter without
the
performance
loss
inherent in purely interpreted languages
It's easy to learn
-
Structure
and
syntax
are
pretty
intuitive
and
easy to grasp
Why do people use Python?
17
Installing Python
Python is pre-installed on most
Unix systems, including Linux
and MAC OS X
But for in Windows Operating Systems
,
user
can
download
from
the
https://www.python.org/downloads/
-
from
the
above
link
download
latest
version
of
python
IDE
and
install,
recent
version
is
3.7.1
but most of them uses version 2.7.7
only
18
After installing the
Python
Ver#3.7.1,
go to start menu
then
click
on
python 3.7 in that
one you can select
python
(command
line) it is prompt
with >>>
19
20
21
22
23
24
25
Installing Python - Testing
26
Installing Python - Android
Go to Google
Play store
Search Python
3 (Pydroid 3)
27
Installing Python - Android
Open Pyroid
via playstore
Open Pyroid
via phone app
28
Installing Python - Android
Open the app and it
will
automatically
install.
Then
type
in
this
command and press the
yellow play button to
test if it’s working.
29
Installing Python – iPadOS
30
Example text
Go ahead and replace it with
your own text. This is an
example text.
PYTHON CORNER: What do you think?
PYTHON EXPLORE: Algorithms & Flowcharts
PYTHON TO GO: Synthesis & Evaluation
Learning Objectives
PYTHON SCENARIO: What is Python?
PYTHON EXPLORE: Algorithms & Flowcharts
31
PYTHON EXPLORE
Algorithms and Flowcharts
A typical programming task can
be divided into two phases:
Problem solving phase
produce an ordered sequence of steps
that describe solution of problem
this sequence of steps is called an
algorithm
Implementation phase
implement the program in some
programming language
32
PYTHON EXPLORE
Algorithms and Flowcharts
Steps in Problem Solving
First
produce
a
general
algorithm
(one can use pseudocode)
Refine
the
algorithm
successively
to
get
step
by
step
detailed
algorithm that is very close to a
computer language.
Pseudocode
is
an
artificial
and
informal
language
that
helps
programmers
develop
algorithms.
Pseudocode
is
very
similar
to
everyday English.
33
PYTHON EXPLORE
Algorithms and Flowcharts
Pseudocode & Algorithm
Example
1:
Write
an
algorithm
to
determine
a
student’s
final
grade
and
indicate
whether
it
is
passing
or
failing.
The
final grade is calculated as
the average of four marks.
34
PYTHON EXPLORE
Algorithms and Flowcharts
Pseudocode & Algorithm
Pseudocode:
Input a set of 4 marks
Calculate their average by
summing and dividing by 4
if average is below 75
Print “FAIL”
else
Print “PASS”
35
PYTHON EXPLORE
Algorithms and Flowcharts
Pseudocode & Algorithm
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 75) then
Print “FAIL”
else
Print “PASS”
endif
36
PYTHON EXPLORE
Algorithms and Flowcharts
The Flowchart
(Dictionary)
A
schematic
representation
of
a
sequence
of
operations,
as
in
a
manufacturing process or computer program.
(Technical)
A
graphical
representation
of
the
sequence
of
operations
in
an
information
system
or
program.
Information
system flowcharts show how data flows from
source
documents
through
the
computer
to
final
distribution
to
users.
Program
flowcharts
show
the
sequence
of
instructions
in
a
single
program
or
subroutine.
Different
symbols
are
used
to
draw each type of flowchart.
37
PYTHON EXPLORE
Algorithms and Flowcharts
A Flowchart
shows logic of an algorithm
emphasizes
individual
steps
and their interconnections
e.g.
control
flow
from
one
action to the next
38
PYTHON EXPLORE
Algorithms and Flowcharts
Basic Flowchart Symbols
Oval
Parallelogram
Rectangle
Diamond
Hybrid
Name
Symbol
Use in Flowchart
Denotes the beginning or end of the program
Denotes an input operation
Denotes an output operation
Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Flow line
Denotes the direction of logic flow in the program
39
PYTHON EXPLORE
Algorithms and Flowcharts
Example 1
PRINT
“PASS”
START
Input
M1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
IS
GRADE<75
PRINT
“FAIL”
STOP
Y
N
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE <75) then
Print “FAIL”
else
Print “PASS”
endif
40
PYTHON EXPLORE
Algorithms and Flowcharts
Example 2
Write
an
algorithm
and
draw
a
flowchart to convert the length in
feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate
the
length
in
cm
(Lcm)
by multiplying LFT with 30
Print length in cm (LCM)
41
PYTHON EXPLORE
Algorithms and Flowcharts
Example 2
Algorithm
Step 1:
Input Lft
Step 2:
Lcm Lft x 30
Step 3:
Print Lcm
START
Input
Lft
Lcm Lft x 30
Print
Lcm
STOP
Flowchart
42
PYTHON EXPLORE
Algorithms and Flowcharts
Example 3
Write an algorithm and draw a flowchart
that
will
read
the
two
sides
of
a
rectangle and calculate its area.
Pseudocode
Input the width (W) and Length (L) of a
rectangle
Calculate the area (A) by multiplying L
with W
Print A
43
PYTHON EXPLORE
Algorithms and Flowcharts
Example 3
Algorithm
Step 1:
Input W,L
Step 2:
A L x W
Step 3:
Print A
START
Input
W, L
A L x W
A
STOP
44
PYTHON EXPLORE
Algorithms and Flowcharts
Example 4
Write
an
algorithm
and
draw
a
flowchart
that
will
calculate
the
roots of a quadratic equation
Hint:
d
=
sqrt
(
),
and
the
roots are:
x1 = (–b + d)/2a
and x2
= (–b – d)/2a
2
0
ax
bx
c
+
+
=
2
4
b
ac
−
45
PYTHON EXPLORE
Algorithms and Flowcharts
Example 4
Pseudocode:
Input the coefficients (a, b, c)
of the quadratic equation
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
46
PYTHON EXPLORE
Algorithms and Flowcharts
Example 4
Algorithm:
Step 1: Input a, b, c
Step 2: d sqrt( )
Step 3: x1 (–b + d) / (2 x a)
Step 4: x2 (–b – d) / (2 x a)
Step 5: Print x1, x2
4
b b
a c
−
START
Input
a, b, c
d sqrt(b x b – 4 x a x c)
Print
x1 ,x2
STOP
x1 (–b + d) / (2 x a)
X2 (–b – d) / (2 x a)
47
PYTHON EXPLORE
Algorithms and Flowcharts
Decision Structures
The
expression
A>B
is
a
logical
expression
it describes a condition we want to
test
if A>B is true (if A is greater than
B) we take the action on left
print the value of A
if A>B is false (if A is not greater
than B) we take the action on right
print the value of B
48
PYTHON EXPLORE
Algorithms and Flowcharts
Decision Structures
is
A>B
Print B
Print A
Y
N
49
PYTHON EXPLORE
Algorithms and Flowcharts
If – Then - Else Structures
The structure is as follows
If condition then
true alternative
else
false alternative
endif
50
PYTHON EXPLORE
Algorithms and Flowcharts
If – Then - Else Structures
The
algorithm
for
the flowchart is as
follows:
If A>B then
print A
else
print B
endif
is
A>B
Print B
Print A
Y
N
51
PYTHON EXPLORE
Algorithms and Flowcharts
Relational Operators
Relational Operators
Operator
Description
>
Greater than
<
Less than
=
Equal to
Greater than or equal to
Less than or equal to
Not equal to
52
PYTHON EXPLORE
Algorithms and Flowcharts
Example 5
Write an algorithm that reads two values,
determines
the
largest
value
and
prints
the
largest
value
with
an
identifying
message.
ALGORITHM
Step 1:
Input VALUE1, VALUE2
Step 2:
if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3:
Print “The largest value is”, MAX
53
PYTHON EXPLORE
Algorithms and Flowcharts
Example 5
MAX VALUE1
“The largest value is”, MAX
STOP
Y
N
START
Input
VALUE1,VALUE2
MAX VALUE2
is
VALUE1>VALUE2
54
PYTHON EXPLORE
Algorithms and Flowcharts
Nested IFs
One of the alternatives
within
an
IF–THEN–ELSE
statement
may
involve
further
IF–THEN–ELSE statement
55
PYTHON EXPLORE
Algorithms and Flowcharts
Example 6
Write an algorithm that
reads three numbers and
prints the value of the
largest number.
56
PYTHON EXPLORE
Algorithms and Flowcharts
Example 6
Step 1: Input
N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1
[N1>N2, N1>N3]
else
MAX N3
[N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2
[N2>N1, N2>N3]
else
MAX N3
[N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
57
PYTHON EXPLORE
Algorithms and Flowcharts
Example 6
Flowchart: Draw the flowchart of the
above Algorithm.
58
PYTHON EXPLORE
Algorithms and Flowcharts
Example 7
Write the algorithm and draw a
flowchart to
a) read an employee name (NAME),
overtime hours worked
(OVERTIME), hours absent
(ABSENT) and
b) determine the bonus payment
(PAYMENT).
59
PYTHON EXPLORE
Algorithms and Flowcharts
Example 7
Bonus Schedule
OVERTIME –
(2/3)*ABSENT
Bonus Paid
>40 hours
>30 but 40 hours
>20 but 30 hours
>10 but 20 hours
10 hours
$50
$40
$30
$20
$10
60
PYTHON EXPLORE
Algorithms and Flowcharts
Example 7
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT 50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT 40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT 30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT 10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
61
PYTHON EXPLORE
Algorithms and Flowcharts
Example 7
Flowchart: Draw the flowchart of the
above algorithm?
62
Example text
Go ahead and replace it with
your own text. This is an
PYTHON CORNER: What do you think?
PYTHON EXPLORE: Algorithms & Flowcharts
PYTHON TO GO: Synthesis & Evaluation
Learning Objectives
PYTHON SCENARIO: What is Python?
PYTHON TO GO: Synthesis & Evaluation
63
Simple coding 1:
In Python 2, the "print"
statement
is
not
a
function,
and
therefore
it
is
invoked
without
parentheses.
However,
in
Python
3,
it
is
a
function,
and
must
be
invoked
with
parentheses.
Example:
Basic Python Programming Activity
NOTE: difference between Python 2 and 3 is the print statement
PYTHON TO GO
64
Simple
coding
4:
Python
is
completely
object oriented, and not "statically typed". You do
not need to declare variables before using them, or
declare their type. Every variable in Python is an
object.
(2)
Strings:
Strings
is
defined
either
with
a
single
quote
or
a
double
quotes.
Basic Python Programming Activity
NOTE: Python basic types of variables
PYTHON TO GO
65
Simple
coding
2:
Python
uses
indentation
for
blocks,
instead
of
curly
braces. Both tabs and spaces are supported,
but
the
standard
indentation
requires
standard Python code to use four spaces.
Example:
Basic Python Programming Activity
NOTE: difference between Python 2 and 3 is the print statement
PYTHON TO GO
66
Basic Python Programming Activity
Python Exercise 1:
Use the "print"
command to print the line "Hello, World!”.
Python
Exercise
2:
Use
the
"print"
command to print the line ”whatever you want to to
say”.
PYTHON TO GO
67
Simple coding 3:
Python
is
completely
object oriented, and not "statically typed". You do
not need to declare variables before using them, or
declare their type. Every variable in Python is an
object. As in every language, a variable is the name
of a memory location. A variable has the type that
corresponds to the value you assign to it Variable
names begin with a letter or an underscore and can
contain letters, numbers, and underscores
(1)
Numbers:
Python
supports
two
types
of
numbers - integers and floating point numbers.
Basic Python Programming Activity
NOTE: Python basic types of variables
Question:
What
is
the
difference
between
an
integers and floating point numbers?
PYTHON TO GO
68
Simple
coding
3:
Python
is
completely
object
oriented,
and
not
"statically
typed".
You
do
not
need
to
declare
variables
before
using
them,
or
declare
their
type.
Every
variable
in
Python is an object.
Integers:
To
define
an
integer,
use
the
following
syntax:
Basic Python Programming Activity
NOTE: Python basic types of variables
PYTHON TO GO
69
Simple
coding
3:
Python
is
completely
object oriented, and not "statically typed". You do
not need to declare variables before using them, or
declare their type. Every variable in Python is an
object.
Floating
number:
To
define
a
floating
point
number,
you
may
use
one
of
the
following
notations:
Basic Python Programming Activity
NOTE: Python basic types of variables
PYTHON TO GO
70
Simple
coding
4:
Python
is
completely
object oriented, and not "statically typed". You do
not need to declare variables before using them, or
declare their type. Every variable in Python is an
object.
(2) Strings:
Strings is defined either with
a single quote or a double quotes.
Basic Python Programming Activity
NOTE: Python basic types of variables
PYTHON TO GO
71
Simple
coding
4:
Python
is
completely
object oriented, and not "statically typed". You do
not need to declare variables before using them, or
declare their type. Every variable in Python is an
object.
(2)
Strings:
Strings
is
defined
either
with
a
single
quote
or
a
double
quotes.
The difference between the two is that using double
quotes makes it easy to include apostrophes (whereas
these
would
terminate
the
string
if
using
single
quotes).
Basic Python Programming Activity
NOTE: Python basic types of variables
PYTHON TO GO
72
Simple
coding
4:
Python
is
completely
object oriented, and not "statically typed". You do
not need to declare variables before using them, or
declare their type. Every variable in Python is an
object.
(2)
Strings:
Strings
is
defined
either
with
a
single
quote
or
a
double
quotes.
Basic Python Programming Activity
NOTE: Python basic types of variables
PYTHON TO GO
73
Strings
Simple
Coding
5:
There
are
additional variations on defining strings that make
it
easier
to
include
things
such
as
carriage
returns, backslashes and Unicode characters. Simple
operators can be executed on numbers and strings:
Basic Python Programming Activity
NOTE: Python basic types of variables
PYTHON TO GO
74
Simple Coding 6:
Assignments can be done on more
than one variable "simultaneously" on the same line like
this:
Basic Python Programming Activity
Simple
Coding
6:
BUT…BUT…BUT…:P
:P
:P
Mixing
operators
between
numbers
and
strings
is
NOT
supported:
PYTHON TO GO
75
Basic Python Programming Activity
Python Exercise 3:
The target of this exercise
is to create a string, an integer, and a floating point number.
The string should be named mystring and should contain the word
"hello". The floating point number should be named myfloat and
should
contain
the
number
10.0,
and
the
integer
should
be
named myint and should contain the number 20
PYTHON TO GO
76
LIST
Simple
Coding
7:
Lists
are
very
similar to arrays. They can contain any type of variable,
and they can contain as many variables as you wish. Lists
can also be iterated over in a very simple manner. Here is
an example of how to build a list.
Basic Python Programming Activity
NOTE:Accessing an index which does not
exist generates an exception (an error).
PYTHON TO GO
77
Basic Python Programming Activity
Python Exercise 4:
In this exercise, you will need
toadd numbers
and strings to the correct
lists using
the
"append"
list
method. You must add the numbers 1,2, and 3 to the "numbers" list, and the
words 'hello' and 'world' to the strings variable.
You will also have to fill in the variable second_name with the second name
in the names list, using the brackets operator [ ]. Note that the index is
zero-based, so if you want to access the second item in the list, its index
will be 1.
PYTHON TO GO
78
Basic Python Programming Activity
Solution
Python
Exercise
4:
Inthis
exercise,
you
will
need
toadd
numbers
and
strings
tothe
correct
lists
using
the
"append" list method. You must add the numbers 1,2, and 3 to the "numbers" list, and
the words 'hello' and 'world' to the strings variable. You will also have to fill in
the variable second_name with the second name in the names list, using the brackets
operator [ ]. Note that the index is zero-based, so if you want to access the second
item in the list, its index will be 1.
PYTHON TO GO
79
Simple Coding 8:
Arithmetic Operators Just as
any
other
programming
languages,
the
addition,
subtraction, multiplication, and division operators can be
used with numbers.
Basic Python Programming Activity
Another operator available is the modulo (%) operator, which returns
the integerremainder of the division. dividend % divisor = remainder.
PYTHON TO GO
80
Simple Coding 8:
Arithmetic
Operators
Using
two
multiplication
symbols
makes
a
power
relationship.
Basic Python Programming Activity
Using Operators with Strings
Python
supports
concatenating
strings
using
the
addition
operator:
PYTHON TO GO
81
Simple Coding 8:
Arithmetic Operators Python
also supports multiplying strings to form a string with a
repeating sequence:
Basic Python Programming Activity
Using Operators with Lists
Lists can be joined with the addition operators:
PYTHON TO GO
82
Simple
Coding
8:
Arithmetic
Operators
Just
as
in
strings,
Python
supports forming new lists with a repeating
sequence using the multiplication operator:
Basic Python Programming Activity
PYTHON TO GO
83
Basic Python Programming Activity
Python Exercise 5:
The target of this exercise is
to create two lists called x_list and y_list, which contain 10 instances of
the variables x and y, respectively. You are also required to create a list
called big_list, which contains the variables x and y, 10 times each, by
concatenating the two lists you have created.
PYTHON TO GO
84
Basic Python Programming Activity
Solution Python Exercise 5:
The target
ofthis
exercise
istocreate
two lists
called
x_list
and
y_list,which
contain 10 instances of the variables x and y, respectively. You are also
required
to
create
alist
called
big_list,which
contains
the
variables x and y, 10 times each, by concatenating the two lists you have
created.
PYTHON TO GO
85
Simple Coding: String Formatting
Python uses C-style string formatting to create new, formatted
strings. The "%" operator is used to format a set of variables
enclosed
in
a"tuple"
(a
fixed
size
list),
together
with
a
format
string,
which
contains
normal
text
together
with
"argument specifiers", special symbols like "%s" and "%d”.
%s - String (or any object with a string representation, like
numbers)
%d - Integers
%f - Floating point numbers
%.<number of digits>f - Floating point numbers with a fixed
amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
Basic Python Programming Activity
PYTHON TO GO
86
Simple Coding 9: String
Formatting
Let's
say
you
have
a
variable
called
"name"
with
your
user
name
in
it,
and
you
would
then
like
to
print(out a greeting to that user.)
Basic Python Programming Activity
What will be the expected output?
PYTHON TO GO
87
Simple Coding 10: String Formatting
Let's
say
you
have
a
variable
called
"name"
with
your
user
name
in
it,
and
you
would
then
like
to
print(out a greeting to that user.)
To use two or more argument specifiers, use
a tuple (parentheses):
Basic Python Programming Activity
What will be the expected output?
PYTHON TO GO
88
Simple Coding 11: String Formatting
Let's say you have a variable called "name" with your user
name
in
it,
and
you
would
then
like
to
print(out
a
greeting to that user.)
Any
object
which
is
not
astring
can
be
formatted
using
the
%s
operator
as
well.
The
string
which
returns
from
the
"repr"
method
of
that
object
is
formatted as the string.
Basic Python Programming Activity
What will be the expected output?
PYTHON TO GO
89
Basic Python Programming Activity
Python Exercise 6:
You
will
need
to
write
a
format
string
which
prints
out
the data using the following syntax: Hello
Eps Doe. Your current balance is Php53.44.
PYTHON TO GO
90
Basic Python Programming Activity
Solution
Python
Exercise
6:
You
will
need
to
write
a
format
string
which
prints
out
the
data
using
the
following
syntax:
Hello
Eps
Doe.
Your
current balance is Php53.44.
PYTHON TO GO
91
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
As you can see, the first thing you learned was printing a
simple sentence. This sentence was stored by Python as a
string.
However,
instead
of
immediately
printing
strings
out,
we
will
explore
the
various
things
you
can
do
to
them. You can also use single quotes to assign a string.
However,
you
will
face
problems
if
the
value
to
be
assigned
itself
contains
single
quotes.
For
example
to
assign the string in these bracket (single quotes are ' ')
you need to use double quotes only like this
92
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
As you can see, the first thing you learned was printing a
simple sentence. This sentence was stored by Python as a
string.
However,
instead
of
immediately
printing
strings
out,
we
will
explore
the
various
things
you
can
do
to
them. You can also use single quotes to assign a string.
However,
you
will
face
problems
if
the
value
to
be
assigned
itself
contains
single
quotes.
For
example
to
assign the string in these bracket (single quotes are ' ')
you need to use double quotes only like this
93
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
SUPAH EPS NOTE: That prints out 12, because "Hello world!" is 12 characters
long, including punctuation and spaces. This time I want you to try this
code.
SUPAH
EPS
NOTE:
That
prints
out
4,because
the
location
of
the
first
occurrence of the letter "o" is 4 characters away from the first character.
Notice
how
there
are
actually
two
o's
inthe
phrase
-this
method
only
recognizes the first.
94
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
SUPAH EPS NOTE: For those of you using silly fonts, that
is a lowercase L, not a number one. This counts the number
of l's in the string. Therefore, it should print 3.
SUPAH EPS NOTE: But why didn't it print out 5? Isn't "o"
the
fifth
character
in
the
string?
To
make
things
more
simple,
Python
(and
most
other
programming
languages)
start things at 0 instead of 1. So the index of "o" is 4.
95
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
SUPAH EPS NOTE: This prints a slice of the string, starting at index 3, and
ending at index 6. But why 6 and not 7? Again, most programming languages
do this - it makes doing math inside those brackets easier.
If you just have one number in the brackets, it will give you the single
character at that index. If you leave out the first number but keep the
colon, it will give you a slice from the start to the number you left in.
If you leave out the second number, if will give you a slice from the first
number to the end.
You can even put negative numbers inside the brackets. They are an easy way
of starting at the end of the string instead of the beginning. This way, -3
means "3rd character from the end”.
96
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
SUPAH EPS NOTE: This prints the characters of string from
3to
7skipping
one
character.
This
is
extended
slice
syntax. The general form is [start:stop:step].
SUPAH EPS NOTE:
Note that both of them produce same output
There is no function like strrev in C to reverse a string. But
with
the
above
mentioned
type
of
slice
syntax
you
can
easily
reverse a string like this
97
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
SUPAH
EPS
NOTE:
The
python
code
will
give
you
a
reversed Hello world! Output.
SUPAH
EPS
NOTE:
These
make
anew
string
with
all
letters
converted
to
uppercase
and
lowercase,
respectively.
98
Simple Coding 12:
Basic String Operations
Strings are bits of text. They can be defined as anything between
quotes:
PYTHON TO GO
PYTHON Skills Evaluation (Hands-on Activity)
SUPAH
EPS
NOTE:
This
is
used
to
determine
whether
the
string
starts with
something or ends with something, respectively. The
first one will print True, as the string starts with "Hello". The
second one will print False, as the string certainly does not end
with "asdfasdfasdf”.
SUPAH
EPS
NOTE:
This
splits
the
string
into
abunch
of
strings
grouped
together
in
alist.
Since
this
example
splits
at
aspace,
the
first
item
in
the
list
will
be
"Hello", and the second will be "world!”.
99
PYTHON TO GO
Basic Python Programming Activity
Python
Exercise
7:
Try
to
fix
the
code
to
out
the
correct
information
by changing the string.
100
PYTHON TO GO
Basic Python Programming Activity
Solution Python Exercise 7:
Try
to
fix
the
code
to
out
the
correct information by changing the string.
101
PYTHON TO GO
Basic Python Programming Activity
Output Python Exercise 7:
Try to
fix the code to print out the correct information by
changing the string.
102
Show answer
Auto Play
Slide 1 / 102
SLIDE
Similar Resources on Wayground
100 questions
[Round-up 4] - Test 6 (1-12) review
Lesson
•
7th Grade
91 questions
Math 7 SOL Review
Lesson
•
7th Grade
95 questions
Review Skills-BOY-7 & 8th Grade
Lesson
•
6th - 8th Grade
93 questions
Industrial Revolution (Lesson Slides)
Lesson
•
8th Grade
101 questions
7.1.2-The Structure of Atoms
Lesson
•
6th - 8th Grade
101 questions
8.ETS.2.1-Science, Technology,-Earth & Space-Lesson/notes
Lesson
•
8th Grade
96 questions
Career Cluster Overload
Lesson
•
6th - 8th Grade
99 questions
Continuity and Unity of Life
Lesson
•
10th - 12th Grade
Popular Resources on Wayground
7 questions
History of Valentine's Day
Interactive video
•
4th Grade
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
22 questions
fractions
Quiz
•
3rd Grade
15 questions
Valentine's Day Trivia
Quiz
•
3rd Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
Discover more resources for Computers
10 questions
Exploring Valentine's Day with Charlie Brown
Interactive video
•
6th - 10th Grade
18 questions
Valentines Day Trivia
Quiz
•
3rd Grade - University
14 questions
Volume of rectangular prisms
Quiz
•
7th Grade
10 questions
Valentine's Day History and Traditions
Interactive video
•
6th - 10th Grade
11 questions
Valentines Day
Quiz
•
6th - 8th Grade
20 questions
Graphing Inequalities on a Number Line
Quiz
•
6th - 9th Grade
25 questions
7th Reading STAAR Vocabulary
Quiz
•
6th - 8th Grade
20 questions
Revising & Editing practice
Quiz
•
7th Grade