Inputting Other Data Types

Jed Rembold

February 1, 2023

Announcements

  • Problem Set 2 due Friday!
    • I’m halfway through PS1 feedback, should be coming back tomorrow
  • A reminder that you can contact your section leaders to ask questions as well!
    • Don’t wait until 11pm on Friday night!
  • I’ll be updating the syllabus schedule, as we added a chapter to the book, but it shouldn’t make a huge difference outside of chapter numbering
    • I’ll also actually fix the old website schedule, since I realized I failed to update that originally
  • Polling: rembold-class.ddns.net

Review Question

Examining the code to the right, what is the output value if I evaluate the expression func2(10)?

  1. 24
  2. 19
  3. 10
  4. 5
def func1(x):
    if x > 10:
        return 3 + x
    else:
        return 0

def func2(y):
    A = func1(y+1)
    if y % 2 == 0 or A ** 3 > 30:
        A -= 5
    return A + y

Shorting the Circuit

  • Python evaluates and and or operators using a strategy called short-circuit mode
  • Only evaluates the right operand if it actually needs to
    • Example: if n=0, then the x % n == 0 is never actually checked in the statement

      n != 0 and x % n == 0

      since n != 0 already is False and False and anything is always False

  • Can use short-circuit to prevent errors: the above x % n == 0 statement would have erred out if n=0

Other Data Types

  • Numbers are great, but what about other types of data?
  • We will spend considerable time on the details of these data types later
  • For now, let us introduce:
    • strings!
    • lists!

Lists

  • A list in Python represents a sequence of any type of data
  • Denote by bordering with square brackets ([, ]) with commas separating each element of the sequence
    • Each element could be any data type (even mixing from element to element!)
    • ['This', 'is', 'a', 'list']
    • ['Great', 4, 'storing', 5 * 10]
  • There are many operations that we will see are possible on lists, but will start with only the basics

Sequences

  • Both strings and lists are examples of a more general type called a sequence
    • Strings are sequences of characters
    • Lists are sequences of anything
  • Sequences are ordered, so we can number off their elements, which we call their index
    • Counting in Python always starts with 0, so the first element of the sequence has index 0
  • Python defines operations that work on all sequences
    • Selecting an individual element out of a sequence
    • Concatenating two sequences together
    • Determing the number of elements in a sequence

Selection

  • You can select or “pluck out” just a single element from a sequence using square brackets [ ]
    • There are no commas between these square brackets, so they can’t be confused with a list
    • The square brackets come after the sequence (or variable name representing a sequence)
    • Inside the square brackets, you place the index number of the element you want to select
>>> A = [2, 4, 6, 8]
>>> print(A[1])
4
>>> B = "Spaghetti"
>>> print(B[6])
't'

Concatenation

  • Concatenation is the act of taking two separate objects and bringing them together to create a single object
  • For sequences, concatenation takes the contents of one sequence and add them to the end of another sequence
  • The + operator concatenates sequences
    • This is why it is important to keep track of your variable types! + will add two integers, but will concatenate two strings
    >>> A = 'fish'
    >>> B = 'sticks'
    >>> print(A + B)
    'fishsticks'
    >>> A = [1, 'fish']
    >>> B = [2, 'fish']
    >>> print(A + B)
    [1, 'fish', 2, 'fish']

Lengths

  • The number of elements in a sequence is commonly called its length, and can be given by the len( ) function

  • Simply place the sequence you desire to know the length of between the parentheses:

    >>> len("spaghetti")
    9
  • You can have sequences of 0 length as well!

    >>> A = ""
    >>> B = [ ]
    >>> print( len(A) + len(B) )
    0

Understanding Check

What would be the printed output of the code to the right?

  1. 12
  2. 13
  3. 14
  4. 15
A = "hots"
B = ["fire", A + A]
C = A[3] + A[1]
B += [C + C[0]]
D = B[0] + B[1] + B[2]
print(len(D))

Input: input

  • To retrieve data from a user, we can use Python’s built-in input() function

  • The form will generally look like:

    variable = input(prompt_text)
    • variable is the variable name you want to assign the user’s typed input to
    • prompt_text is the string that will be displayed on the screen to communicate to the user what they should be doing
  • The input() function always returns a string

    • If you want to get an integer from the user, you will need to convert it yourself after retrieving it

      num = int(input('Pick a number between 1 and 10: '))

Running a Program

  • Python programs specify what part of the code is supposed to be executed when a program is run using a few special lines at the end of the program

    if __name__ == '__main__':
        function_to_run()
    • function_to_run is the name of whatever function you want to execute when the program is run directly
  • Patterns of this sort are commonly called boilerplate

    • Less important to fully understand all the pieces of it now
    • Is important to understand what it is doing and how to implement it correctly

An adding program

# File: AddTwoIntegers.py

"""
This program adds two integers entered by the user.
"""

def add_two_integers():
    print("This program adds two integers.")
    n1 = int(input("Enter n1? "))
    n2 = int(input("Enter n2? "))
    total = n1 + n2
    print(f"The sum is: {total}")

# Startup boilerplate
if __name__ == '__main__':
    add_two_integers()

Common Patterns

  • Many programs in programming can be approached similarly by using a particular idiom or pattern
  • Using a variable to track/control a loop state

    finished = False
    while not finished:
        line = input("Enter a number: ")
        if line == "":
            finished = True
        else:
            print(line)
  • Building up a sequence from nothing using concatenation

    new = ""
    word = "fantastical"
    i = 0
    while i < len(word):
        new += word[i]
        i += 2

Visiting the library(ies)

  • A huge strength of Python is that it offers a very large number of code collections called libraries
    • Can save you the effort from writing that code yourself!
  • Requires you to import that library, which can take several different forms
    • Most common is to use import to grab everything in a library

      import math
      • You must then access any function in that library using its fully qualified name, which includes the library name (var = math.sqrt(4))
    • Can also use from ... import to grab specific functions from the library

      from math import sqrt
      • You do not need to use the library name then when you call it (var = sqrt(4))

Useful math definitions

Code Description
math.pi The mathematical constant \(\pi\)
math.e The mathematical constant \(e\)
math.sqrt(x) The square root of x
math.log(x) The natural logarithm of x
math.log10(x) The base 10 logarithm of x
math.sin(x) The sine of x in radians
math.cos(x) The cosine of x in radians
math.asin(x) The arcsin of x
math.degrees(x) Converts from radians to degrees
math.radians(x) Converts from degrees to radians
// reveal.js plugins