Lecture 9 Flashcards

1
Q

Two methods to input data:

A

-prompt the user to input data while the program is running
-provide data at the program start unprompted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Input()

A

Is a built in python function that pauses the program and waits for the user to input some data. The user can can submit data by thping it into the terminal window and pressing enter on the keyboard

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is user input returned

A

It is returned as a string which may need to be converted to another tupe depending on how its used in the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Input example:

A

name = input(“What is your name? ”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

name = name.split()

A

Splits name at the spaces, returns a list of all the strings between spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Comma_nums = input(“Provide 4 numbers separated by commas: “)
comma_num = comma_nums.split(“,”,3)

A

Splits the ‘comma_nums’ at the “,”. Does a max of 3 splits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

back_to_back_nums = input(“Provide 4 numbers back to back: “)
back_to_back_nums =lust(back_to_back_nums)

A

Returns a list of all three characters in back to back nums

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Trouble shooting sys.argv

A

If the python file is not in the same folder as your current directory you need to do one of two things:
1) change your current directory
2) include the path to the python file when trying to run the program
If the python is not on the path of your computer, include the path to python file when trying to run your program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Output

A

There are many ways to output data from a program(print to terminal screen print to file send to another program send to output
Here we will expand print to the terminal screen by learning how to properly format strings for print()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

An f string is a formatted string that allows us to:

A
  • embed variables and simple expressions directly into the string
    -call functions within strings
    -specify ways to display numeric data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Greeting= f”Hello World!”
Print(greeting)
Age= 19
Print(f”Dean os {age} years old.”)

A

Hello World
Dean is 19 years old.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Price = 25
Print(f”The price is {price:.2f}”)

A

The price is 25.00

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Price = 25
Print(f”The price is {price:b}”)

A

11001

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Price = 25
Print(f”The price is {price:x}”)

A

19

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Price = 25
Print(f”The price is {10000000*price:5,.2f}”)

A

25,000,000.00

17
Q

Price = 25
Print(f”The price is {1000000*price:15,.2f}”)

A

The price is 25,000,000.00

18
Q

General syntax for float formatting: {value:{width}.{precision}}

A
  • width is the minimum number of characters used to display the number
    -precision is the number of characters used to display after the decimal
    -Python will round a float when deciding what to display
19
Q

def balance(amount, cost):
return amount-cost
Unit price = 25
Num_units = 3

Print(f”the total cost is {unit pricenum_units:.2f}”)
Print(f”The total cost is {‘Reasonable’ is unit_price
number_units <50 else ‘unreasonable’}”)
Print(f”You have {balance(100, unit_price*num_units):.2f} dollars remaining”)

A

The total cost is 75.00
The total cost is unreasonable
You have 25.00 dollars remaining

20
Q

Print(f”|{‘col1’:<10}|{‘col2’:^10}|{‘col3’:>10}|”)

A

10 spaces to the right of column, 5 on either side of column 2, 10 to the left of column 3

21
Q

\n

22
Q

\t

A

Tab character, moves to next tab every 8 characters

24
Q

back_to_back_nums = input(“Provide 4 numbers back to back:”)
Back_to_back_nums = list(back_to_back_nums)

A

Returns a list of all the characters in back_to_back_nums

25