Python Chapter 2 PT 2

Python Chapter 2 PT 2

CHAPTER 2 Input, Processing , and Output Suppressing print Space print function uses a space to separate items in print statement To suppress the print between items printed use Special argument sep='delimiter'

Causes print to use delimiter as item separator Delimiter is usually no space in this case , but can be anything >>> print (One,Two,Three, sep=) Output: OneTwoThree Suppressing print Newline print function usually goes to next line when done A feature allows you to suppress newline Argument end='delimiter' causes print to place delimiter at end of print output

Delimiter can be what you want, Usual delimiters Is set to Space : Or no space: Print(One, end= ) Print(Two, end= ) Print(Three) Output One Two Three Escape Characters Special characters appearing in string literal

Preceded by backslash (\) Examples: newline (\n), horizontal tab (\t) Treated as commands inside a string print(One\nTwo\nThree) Output: One Two Three print(Mon\tTue\tWed) print(Thu\tFri\tSat) Output: Mon

Tue Wed Thu Fri Sat Stripping Whitespace Whitespace is any nonprinting character Examples: Spaces, tabs, end-of-line symbols Spaces in a string can be confusing to a program Ex: 'python ' and 'python' look the same to humans, but not to Python. It sees the extra space at the end.

Useful when user adds space at end of username Can look for extra spaces on right and left of string rstrip() removes spaces on right of string lstrip() removes spaces on left of string strip() removes spaces from both sides Stripping Whitespace Example fav_language = 'python ' # Notice extra space print(fav_language.rstrip()) Produces

Python with no extra space fav_language = ' python ' # Notice extra spaces print (fav_language.strip()) Produces Python with no spaces on either side Formatting Numbers format function: can format floating point

numbers up to 12 significant digits format(numeric_value, format_specifier) Returns string containing formatted number Format specifier typically includes precision and data type Format: decimal_width(type) Ex: 2f for 2 digits after decimal point Numbers >= 5 are rounded up >>> print(format(123.4567, .2f) 123.46 Comma Separator

Allows you to include commas in numbers >999 Place a comma before the . in the formatting string Ex: ,.2f big_number = 12568.789 print (format(big_number,',.2f')) Output: 12,568.79 Formatting Numbers Examples # This program demonstrates how a floating-point # number can be displayed as currency.

# Notice the use of sep= so $ is next to number monthly_pay = 5000.0 annual_pay = monthly_pay * 12 print('Your annual pay is $', \ format(annual_pay, ',.2f'), sep='') Output: Your annual pay is $60,000.00 Setting Number Width Determine width of number: Format: '[field_width] decimal_width(type)' Ex: '12.2f' will create a 12 character wide floating point number with two digits after the

decimal Formatting Field Width Example num1 = 127.899 num2 = 3465.148 num3 = 3.776 num4 = 264.821 # Display each number in a field of 7 spaces # with 2 decimal places. print(format(num1, '7.2f')) print(format(num2, '7.2f')) print(format(num3, '7.2f')) print(format(num4, '7.2f'))

Output: 127.90 3465.15 3.78 264.82 Formatting Number as Percentage The % symbol can be used in the format string to format a number as a percentage Percentages are usually fractions with numbers after the decimal place, but they are written as if you multiplied that number by 100 Ex: 0.05 is 50%

>>> print(format(0.5, %)) # Default width 50.000000% >>> print(format(0.5, 0%)) # has no digit after # decimal 50% Formatting Integers To format an integer using format function: Use d as the type designator Do not specify precision Can set field width or comma separator >>> print(format(123456,d)) 123456

>>> print(format(123456,,d)) 123,456 Undefined Constant An unexplained numeric value that appears in a programs code. Example: amount = balance * 0.069 What is the value 0.069? An interest rate? A fee percentage? Only the person who wrote the code knows for sure. interest_rate = 0.069 amount = balance * interest_rate

Reason To Use Constants It can be difficult to determine the purpose of the number. It can take a lot of effort to change the number in multiple locations. Risk of making a mistake each time you type the number in the programs code. Ex: suppose you intend to type 0.069, but you accidentally type .0069. This mistake will cause mathematical errors that can be difficult to find.

Recently Viewed Presentations

  • Section 30.2 Other Galaxies in the Universe Objectives

    Section 30.2 Other Galaxies in the Universe Objectives

    The Hubble tuning-fork diagram summarizes Hubble classification for normal galaxies. Classification of galaxies. Other Galaxies in the Universe. Section 30.2. Some galaxies do not have distinct shapes and are referred to as irregular galaxies.
  • Chapter 1

    Chapter 1

    Chapter 14. Integration of the nervous system functions. Compared to animals we have large complex brains that have the same basic function of receiving and sending signals, but we are also capable to unique complex functions: recording history, reasoning, planning,...
  • www.npsd.k12.nj.us

    www.npsd.k12.nj.us

    Around 509 BC, the Romans defeated the Etruscans and took control of their city. They created a form of government called a republic where citizens had the right to vote for their leaders . By 264 BC, Romans controlled all...
  • STARDUST STARDUST Project CRITICAL EVENTS READINESS REVIEW COMET

    STARDUST STARDUST Project CRITICAL EVENTS READINESS REVIEW COMET

    CIDA timing value; CIDA needs all updates to encounter time to set the instrument encounter mode correctly. ... Risk Rating (Red/Yellow/Green) - this rating is essentially a gut-level assessment based on the impact and likelihood of the risk . 8....
  • A Great Partnership Enterprise Rent-A-Car and State of

    A Great Partnership Enterprise Rent-A-Car and State of

    A Great Partnership Enterprise Rent-A-Car and State of Indiana Customer Satisfaction It is a business strategy Required to succeed Measured extensively 2,000,000 customers surveyed annually Business Success Largest Rent-A-Car Company Indiana Largest Rent-A-Car Company in Indiana The Right Vehicle… 120...
  • Intermolecular Forces - Ursuline High School

    Intermolecular Forces - Ursuline High School

    5% of the strength of a covalent bond Results in higher b.p. and high viscosity e.g. H2O Comparing All Bond Types Which is stronger? metallic > ionic > covalent > H bond > other dipole > dispersion e.g. Compare melting...
  • Introduction - National Chengchi University

    Introduction - National Chengchi University

    Switched network Interconnection of networks * Strategies Circuit switching original telephone network carry bit streams Packet switching store-and-forward messages each node first receives a complete packet over some link stores the packet in its internal memory forwards the complete packet...
  • Teen Pregnancy in Kent County, MI

    Teen Pregnancy in Kent County, MI

    Sex education curriculums can assess direct measures of teen comprehension regarding pregnancy by incorporating learning tools such as term papers, presentations, and class projects. Indirect measures of teen comprehension of pregnancy in Kent County, MI would be evaluated by city...