0% found this document useful (0 votes)
8 views3 pages

4 2mod7

asfasdfa

Uploaded by

llipka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

4 2mod7

asfasdfa

Uploaded by

llipka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

3/6/23, 11:55 AM 40460_Mod04_4.2 (1) (1).

ipynb - Colaboratory

Section 4.2
Nested Conditionals
Nested conditionals
Print formatting with the () escape sequence

Student will be able to


Create nested conditional logic in code
Print format print using escape sequence ()

Concept: Formatting with Escape Sequences


[![view video] (https://drive.google.com/file/d/1OztbVhbGxAWBZv_yYLXadxJDEfDCRNVB/view?usp=sharing)

Escape Sequences
Escape sequences all start with a backslash ( \ )
Escape sequences can be used to display characters in Python reserved for formatting

\\ Backslash ( \ )
\' Single quote (')
\" Double quote (")

Escape sequences are part of special formatting charcters\n Linefeed

\t Tab
\n return or newline

We use escape sequences in strings - usually with print() statements.

Examples

# review and run example using \n (new line)


print('Hello World!\nI am formatting print ')

Hello World!
I am formatting print

# review and run code using \t (tab)


student_age = 17
student_name = "Hiroto Yamaguchi"
print("STUDENT NAME\t\tAGE")
print(student_name,'\t' + str(student_age))

STUDENT NAME AGE


Hiroto Yamaguchi 17

# review and run code


# using \" and \' (escaped quotes)
print("\"quotes in quotes\"")
print("I\'ve said \"save your notebook,\" so let\'s do it!")

# using \\ (escaped backslash)


print("for a newline use \\n")

"quotes in quotes"
I've said "save your notebook," so let's do it!
for a newline use \n

Task 1: Format using backslash ( \ ) escape sequences

https://colab.research.google.com/drive/1ReOVE-6HJPBvkU2ymbqQqKheBiEUBkBs#scrollTo=qAB-zaRayrdq&printMode=true 1/3
3/6/23, 11:55 AM 40460_Mod04_4.2 (1) (1).ipynb - Colaboratory
# [ ] print "\\\WARNING!///"

print("\\\WARNING!///")

\\WARNING!///

# [ ] print output that is exactly (with quotes): "What's that?" isn't a specific question.
print("\"What's that?\"isn't a specific question")

"What's that?"isn't a specific question

# [ ] from 1 print statement output the text commented below using no spaces
# One Two Three
# Four Five Six

print("One\tTwo\tThree\nFour\tFive\tSix")

One Two Three


Four Five Six

Task 2: Program: pre_word() function


Function has a single string parameter that it checks is a single word starting with "pre".

Check if word starts with "pre"


Check if word .isalpha()
If all checks pass: return True
If any checks fail: return False
Test

Get input using the directions: *enter a word that starts with "pre": *
Call pre_word() with the input string
Test if return value is False and print message explaining not a "pre" word
else print message explaining is a valid "pre" word

# [ ] create and test pre_word()

def pre_word(check):
if check.isalpha:
print("Is alphabetical")
else:
print("Is not alphabetical")
if check.startswith("pre"):
print("Starts with pre")
else:
print("Doesn't start with pre")
return check

check1=input("What word should we check?")

pre_word(check1)

What word should we check?presahas


Is alphabetical
Starts with pre
'presahas'

Task 3: Fix the errors

# [ ] review, run, fix


print("Hello\nWorld!")

Hello
World!

https://colab.research.google.com/drive/1ReOVE-6HJPBvkU2ymbqQqKheBiEUBkBs#scrollTo=qAB-zaRayrdq&printMode=true 2/3
3/6/23, 11:55 AM 40460_Mod04_4.2 (1) (1).ipynb - Colaboratory

Terms of use Privacy & cookies © 2017 Microsoft

Created in Deepnote

check 0s completed at 11:55 AM

https://colab.research.google.com/drive/1ReOVE-6HJPBvkU2ymbqQqKheBiEUBkBs#scrollTo=qAB-zaRayrdq&printMode=true 3/3

You might also like