CS 110: Computer Science Fundamentals Week 4 PDF
Document Details
Uploaded by Deleted User
Tags
Related
- Python Game Coding Level 1 Learner Resource Lesson 1 PDF
- PyGame Lesson 2 Learner Resource PDF
- Python Programming - Introduction to Computational Thinking PDF
- Présentation Cours Informatique 03 (Découverte de Python et de l'instruction d'affichage) PDF
- Python Programming Lesson 1 PDF
- CS 1400 Final Exam PDF
Summary
This document is an excerpt from a Computer Science Fundamentals course lecture notes likely from week 4, focusing on Python programming concepts, and demonstrates code examples and concepts relating to print functions, string formatting, and multi-line strings.
Full Transcript
CS 110: Computer Science Fundamentals Week 4: Variables and Values III 1 Controlling the print Function print (’Please enter an integer value:’) x = input () print (’Please enter an integer value:’) y = input () num1 = int(x) num2 = int(x) print (num1 ,’+’,num2 , ’=’...
CS 110: Computer Science Fundamentals Week 4: Variables and Values III 1 Controlling the print Function print (’Please enter an integer value:’) x = input () print (’Please enter an integer value:’) y = input () num1 = int(x) num2 = int(x) print (num1 ,’+’,num2 , ’=’, num1 + num2) The print function prints a line of text, and then the cursor moves down to the next line so any future printing appears on the next line. The print function accepts an additional argument that allows the cursor to remain on the same line as the printed text. A keyword argument expression, end =, can be added at the end of the printing text after a comma to keep the text in the same line. print (’Please enter an integer value:’, end =’’) x = input () The keyword argument expression that separates between items is sep =. w, x, y, z = 10, 15, 20, 25 print (w, x, y, z, sep =’,’) 10 ,15 ,20 ,25 Practice: Print w, x, y, and z using the following arguments. sep = ’’ sep = ’:’ sep = ’ ∗ ∗ ∗ ’ 2 String Formatting Consider the following program, which prints the first few powers of 10. print (6, 10 ∗ ∗ 6) print (7, 10 ∗ ∗ 7) print (8, 10 ∗ ∗ 8) 1 Try the following program. print (’ { 0 } { 1 } ’. format (7, 10 ∗ ∗ 7)) print (’ { 0 } { 1 } ’. format (8, 10 ∗ ∗ 8)) print (’ { 0 } { 1 } ’. format (9, 10 ∗ ∗ 9)) Each statement has two expressions: ’{0} {1}’ and format(7, 10**7). ’{0} {1}’ is known as the formatting string. It is a string, but Python does not print it. It is a placeholder, also known as positional parameters, to be replaced with other objects. format(7, 10**7) provides arguments to be substituted into the formatting string by the corresponding order. How about this? x, y = 5, 6 print (’a { 0 } b { 1 } c { 0 } { d } ’. format (x, y)) If the positional parameter has ’{0:>3}’, it means right-justify the first argument to format within a width of three characters. Practice: Print 10**7, 10**8, and 10**9 as right-justified within 10 places. Use ’=’ instead. How do outputs print? 3 Multi-line Strings In Python, it is illegal to have a string that spans more than a single line. For example, x = ’This is a long string with several words and multiple lines ’ A string literal that begins with a ’ or " must be terminated with its matching ’ or " on the same line in which it begins. A newline control code \n can be added to produce line breaks within the string. Python provides a way to represent a string’s layout more naturally within source code using triple quotes (”’ or """). 2