Homework5

pdf

School

San Jose State University *

*We aren’t endorsed by this school

Course

30

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

4

Uploaded by MinisterBravery13388

Report
Homework 05 Strings Harpreet Rathore 03/08/2022 Problem 5.1 - Escape Characters What are escape characters? Why do we need them? An escape character is a backslash \ followed by the character you want to insert and it is used to insert characters that are illegal in a string. Problem 5.2 - More Escape Characters What do the \n and \t escape characters represent? The \n character is called a new line character and it breaks a string into a new line just like hitting enter when typing regularly. The \t character is called a tab character and it puts indentation in between a string. Problem 5.3 - The Backslash Character How can you put a \ backslash character in a string? I am the "best" when it comes to Python. Problem 5.4 - Single and Double Quotes The string value "Howl's Moving Castle" is a valid string. Why isn’t it a problem that the single quote character in the word Howl's isn’t escaped? Howl's Moving Castle It is valid to have a single quotation character in a double quoted string and vice versa without escape command, but the type of quotations cannot be the same. Problem 5.5 - Print Statement In [1]: String = "I am the \"best\" when it comes to Python." print ( String ) In [6]: Value = "Howl's Moving Castle" print ( Value )
Printout the following statement: Hello, my name is your name here and I am your age here old. Define two variables, my_name and my_age and use two different ways of printing out your statement: 1. Using string concatenation. 2. Use the f-string method. See how much easier the f-string method is! Hello, my name is Harpreet Rathore and I am 21 years old Hello, my name is Harpreet Rathore and I am 21 years old I find the f-string method to be a lot easier as I can shorten my code and it is easily readable. Problem 5.6 - Useful String Methods Consider the string: str1 = 'the Hero gets IN trouble.' Now use string methods to accomplish the following: 1. Capitalize just the first letter of the sentence 2. Convert the string entirely to upper case 3. Convert the string entirely to lower case 4. Return a Boolean which returns True if the substring str1[4:7] is all in lower case 5. Return the index value where the substring 'ets' begins with respect to the original string. The hero gets in trouble. THE HERO GETS IN TROUBLE. the hero gets in trouble. False 10 Problem 5.7 - Print Formatting Use the f-string method to print the values of the physical constants pi and e. Use the math library and use math.pi and math.e for the two constants respectively. The neatly formatted table that should look the In [14]: my_name = "Harpreet Rathore" my_age = "21" sentence = "Hello, my name is " + my_name + " and I am " + my_age + " years old" print ( sentence ) txt = "Hello, my name is {} and I am {} years old" print ( txt . format ( my_name , my_age )) In [16]: str1 = 'the Hero gets IN trouble.' print ( str1 . capitalize ()) #1 print ( str1 . upper ()) #2 print ( str1 . lower ()) #3 print ( str1 [ 4 : 7 ] . islower ()) #4 print ( str1 . find ( 'ets' )) #5
following cell, with each constant displayed up to five decimal places: pi e ----------------- The values are: 3.14159 2.71828 pi e ----------------- 3.14159 2.71828 The CPX Board. Buttons - D4 and D5 Button A is on the left and button B is on the right. These buttons can be used as inputs, which means you can use them to tell your board to do something when you press them. When Button A is pressed, cp.button_a returns True ( False otherwise). Likewise, cp.button_b returns True when Button B is pressed. Problem 5.8 Write a program to turn the 0th NeoPixel (RGB LED) green while Button A is pressed and blue while Button B is pressed. When no button is pressed, no LED should be lit. Remember to put your code in an infinte while loop by using the statement while True , and put a small time delay in each branch of the conditional. Take a short video for submission - follow the method outlined for HW04 This was the code used for Problem 5.8 in Mu editor Led brightness was set at 0.1 The time delay was set at 1 second In [1]: import math X = math . pi Pi = float ( "{0:.5f}" . format ( X )) Y = math . e E = float ( "{0:.5f}" . format ( Y )) txt = " pi e\n-----------------\n{} {} " print ( txt . format ( Pi , E )) In [ ]: import time from adafruit_circuitplayground import cp cp . pixels . brightness = 0.1 while True : if cp . button_a == True : time . sleep ( 1 ) cp . pixels [ 0 ] = ( 0 , 255 , 0 ) elif cp . button_b == True : time . sleep ( 1 ) cp . pixels [ 1 ] = ( 0 , 0 , 255 )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Submission Please submit a .ipynb file and a .pdf file.