concept True in category python

appears as: True
Tiny Python Projects: Learn coding and testing with puzzles and games

This is an excerpt from Manning's book Tiny Python Projects: Learn coding and testing with puzzles and games.

  • Flags --A Boolean value like “yes”/“no” or True/False is indicated by something that starts off looking like a named option, but there is no value after the name; for example, the -d or --debug flag to turn on debugging. Typically the presence of the flag indicates a True value for the argument, and its absence would mean False, so --debug turns on debugging, whereas its absence means it is off.
  • Figure 2.3 The if expression will return the first value if the predicate is True and the second value otherwise.

    If any of the items in a given list is “truthy,” the whole expression will evaluate to True:

    Python Workout: 50 ten-minute exercises

    This is an excerpt from Manning's book Python Workout: 50 ten-minute exercises.

    Now that the computer has chosen a number, it’s the user’s turn to guess what that number is. Here, we start an infinite loop in Python, which is most easily created with while True. Of course, it’s important that there be a way to break out of the loop; in this case, it will be when the user correctly guesses the value of answer. When that happens, the break command is used to exit from the innermost loop.

    Table 4.1 What you need to know (view table figure)

    Concept

    What is it?

    Example

    To learn more

    input

    Prompts the user to enter a string, and returns a string.

    input('Enter your name: ')

    http://mng.bz/wB27

    dict

    Python’s dict type for storing key-value pairs. dict can also be used to create a new dict.

    d = {'a':1, 'b':2} or d = dict'a', 1), ('b', 2

    http://mng.bz/5aAz

    d[k]

    Retrieves the value associated with key k in dict d.

    x = d[k]

    http://mng.bz/5aAz

    dict.get

    Just like d[k], except that it returns None (or the second, optional argument) if k isn’t in d.

    x = d.get(k) or x = d.get(k, 10)

    http://mng.bz/4AeV

    dict.items

    Returns an iterator that returns a key-value pair (as a tuple) with each iteration.

    for key, value in d.items():

    http://mng.bz/4AeV

    set

    Python’s set type for storing unique, hashable items. set can also be used to create a new set.

    s = {1,2,3} # creates a 3-element set

    http://mng.bz/K2eE

    set.add

    Adds one item to a set.

    s.add(10)

    http://mng.bz/yyzq

    set.update

    Adds the elements of one or more iterables to a set.

    s.update([10, 20, 30, 40, 50])

    http://mng.bz/MdOn

    str.isdigit

    Returns True if all of the characters in a string are digits 0-9.

    '12345'.isdigit()

    # returns True

    http://mng.bz/oPVN

    Get Programming: Learn to code with Python

    This is an excerpt from Manning's book Get Programming: Learn to code with Python.

    True

    Write a function named common. It takes in two parameters, lists named L1 and L2. The function doesn’t mutate L1 or L2. It returns True if every unique element in L1 is in L2 and if every unique element in L2 is in L1. It returns False otherwise. Hint: try to reuse your function from Q25.2. For example,

  • common([1,2,3], [3,1,2]) returns True
  • common([1,1,1], [1]) returns True