Chapter 2: Types, Variables, and Simple I/O - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 2: Types, Variables, and Simple I/O

Description:

Chapter 2: Types, Variables, and Simple I/O CS10061 Introduction to Computer Programming Kent State University – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 32
Provided by: George834
Learn more at: https://www.cs.kent.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2: Types, Variables, and Simple I/O


1
Chapter 2 Types, Variables, and Simple I/O
  • CS10061 Introduction to Computer Programming
  • Kent State University

2
Topics Covered January 30
  • More about strings and text
  • Working with triple quotes
  • Using shortcuts and escape sequences
  • Concatenating and repeating strings
  • Working with string operators
  • Working with numbers
  • Understanding variables
  • Using string methods

3
More About Strings and Text
  • Quotes inside strings
  • Quotes are required at the beginning of a string
    and at the end of the string
  • Quotes may take three forms
  • Single (one statement, one line)
  • Double (one statement, one line)
  • Triple (one statement, multiple lines, display as
    seen)
  • Quotes must be consistent

4
More About Strings and Text
  • To mark a quote within a string, use the kind of
    quotes not used for the string itself, see the
    following
  • Remember, remain consistent or a syntax error
    will result

print This is a quotation within the
double-quoted string.
The string is enclosed in the double quotes, and
the quotation is enclosed in the single quotes.
5
More About Strings and Text
Use care when enclosing the quotation inside the
string. If the same kind of quotes are used for
the quotation that are used for the string, a
syntax error will result. Why does this happen?
6
Working with Triple Quotes
Note that the triple quotes around the string
have no blank lines preceding or following them.
7
Working with Triple Quotes
Note that the first triple quote is on its own
line but the last triple quote is on the same
line as the string.
8
Working with Triple Quotes
Note that both the beginning and ending triple
quotes are on their own lines, but the ending
triple quote and the string are separated by a
blank line.
9
Using Shortcuts and Escape Sequences
  • Line-continuation character (\)
  • Use to stretch a very long statement across
    multiple lines
  • The interpreter will see it as one continuous
    statement
  • Example

print This is a very long statement that will
stretch \ across multiple lines but will print on
one single \ line when displayed.
10
Using Shortcuts and Escape Sequences
  • System bell (\a)
  • Sounds the system bell if invoked by the
    operating system (double-click file)
  • Prints a square box if run through IDLE
    (interactive and script)
  • Use only once, multiple times in a sequence, or
    inside a string
  • Example

print \a or print \a\a\a
or print Listen to the\a bell ring!
11
Using Shortcuts and Escape Sequences
  • Tab stop (\t)
  • Indents the string text from the left margin
  • Nearly centers text on the screen if used
    multiple times in a sequence
  • Example

print \tMove this sentence one tab stop from the
left. print \t\t\t\t\tNearly center this on
the screen.
12
Using Shortcuts and Escape Sequences
  • Backslash (\\)
  • Prints a single backslash even though two
    backslashes are shown
  • Separate multiple backslashes with spaces
  • Example

print \\ will display one backslash print \\
\\ \\ \\ displays four backslashes separated by
spaces
13
Using Shortcuts and Escape Sequences
  • Newline (\n)
  • Commonly used to separate one string from
    subsequent strings
  • Also accomplished with an expressionless print
    statement
  • Example

print \nPrint a new line and then this
statement. print
14
Using Shortcuts and Escape Sequences
  • Inserting quotations within strings
  • Eliminates the ambiguity seen with using just
    single or double quotes
  • Clearly defines the quotations and how they are
    to be displayed
  • As before, use at the beginning and the end of
    the embedded quote

15
Using Shortcuts and Escape Sequences
  • Single quotes (\)
  • Recall that we could handle embedded quotes just
    by using a single quote
  • Now we can do the same thing using an escape
    sequence single quote

print The string with the embedded quote
inside.
print The string with \the embedded quote\
inside.
16
Using Shortcuts and Escape Sequences
  • Double quotes (\)
  • Recall that we could handle embedded quotes by
    using double quotes also
  • Now we can do the same using an escape sequence
    double quote

print The string with the embedded quote
inside.
print The string with \the embedded quote\
inside.
17
Working with String Operators
  • Concatenating strings is joining multiple (gt2)
    strings together. The result is a new string.
  • The addition operator () performs the
    join
  • Example string1 string 2
  • The strings are added together (like
    numbers)

18
Working with String Operators
  • Special notes ()
  • No space inserted at the join
  • If a space is needed, then insert
  • After string 1
  • Before string 2
  • Line-continuation characters are needed for
    multiple joins
  • Expect incorrect text wrapping

19
Working with String Operators
  • Repetition operator ()
  • Used to repeat a string a specific number of
    times
  • The string repetitions print on one line with no
    separating spaces
  • The strings are multiplied together (like
    numbers)

20
Newline Suppression
  • Recall that \n is an escape sequence that
    inserts blank lines and is used with multiple
    print statements
  • What if you want the multiple print statements to
    appear on the same line?
  • Use the newline suppression (,) at the end of the
    first print statement

21
Working with Numbers
  • Two most common numbers used and manipulated in
    programs
  • Integers (whole numbers)
  • Floating-point (decimal point or fractional)
  • String values are used in Python. What other
    values are used? Numbers!
  • Much like string text, numbers are considered to
    be expressions and are evaluated that way by the
    interpreter

22
Working with Numbers
  • Mathematical operators used
  • Addition ()
  • Subtraction (-)
  • Multiplication ()
  • Division (/)
  • Modulus ()
  • Calculates the remainder (not quotient) from
    division

Use all of these operators with integer numbers
or floating-point numbers.
23
Working with Numbers
  • Special notes (/)
  • True division is not performed on integers
  • Integer division is performed on integers
  • Fractional part of quotient is ignored
  • To perform true division, change one of the
    integers to a floating-point
  • Example 9 / 4 becomes 9.0 / 4 or 9 / 4.0
  • Repeating decimals that result from
    floating-point division are rounded, but not
    accurately

24
True Division vs. Integer Division
Integer division quotients fractional part is
ignored.
For true division, change either the dividend or
the divisor to a floating-point number.
25
Understanding Variables
  • Variables are areas within memory used to get,
    store and manipulate program values.
  • They are created through assignment statements
    which do two things
  • Creates and gives the variable a name
  • Assigns the variable a value
  • Examples number 7
  • prompt Press enter to
    begin.

26
Understanding Variables
  • Variable naming conventions
  • May not begin with a number although numbers,
    letters, and underscores are allowed
  • Be descriptive for clarity and subsequent code
    modifications
  • Be consistent, especially for multiword names
    (separate by an underscore)

27
Understanding Variables
  • Variable naming conventions contd
  • Follow language traditions
  • No preceding underscores (reserved)
  • Begin with a lowercase letter
  • Limit name lengths to lt 15
  • Underscores are not helpful
  • Leads to ambiguity
  • Practice implementing self-documenting code
    (understandable without comments)

28
Using String Methods
  • Values used in programs usually have certain
    abilities associated with them.
  • These abilities are mini-programs called methods.
  • For string values, we may use several methods
  • Uppercase and lowercase
  • Capitalization and title representation
  • Swaps, strips, and replacements

29
Using String Methods
  • Methods take the following form
  • The dot will invoke the method
  • The parentheses make a call or pass arguments to
    the method

Value.Name of Method()
30
Using String Methods
  • Methods can not be called on their own
  • The value associated with the method must make
    the call
  • The main difference between methods and functions
  • Be sure that the arguments are included, if
    necessary, or an error will result (see the
    following)

31
Using String Methods
The method replace takes two arguments and one
of them is missing from the statement.
The interpreter tells us about the missing
argument with the TypeError message that is given.
Write a Comment
User Comments (0)
About PowerShow.com