Chapter 3 python

Ace your homework & exams now with Quizwiz!

ord(a), chr(97)

97,a

Variable

A variable is a memory location in which a value may be stored. Most variables have an associated name which is used to reference the variable.

round(x)

Returns an integer nearest to x, if x is equally close to two integers, the even one is returned

min(x1, x2, ...)

Returns the smaller among x1, x2

sqrt(x)

Returns the square root of x

Associate the variable foreground with the value "red".

foreground = "red"

Write code to assign to the variable format a formatting string that will display three values referenced by the variables quantity (type int), description (type str), and unitPrice (type float) in the format illustrated by: (type "_10 One-inch Three-ring binder_____________ $___5.50"). The quantity is displayed right-justified in 3 print positions, the description is displayed left-justified in 30 print positions, and the amount is displayed after a dollar sign with a total of 7 print positions and two digits after the decimal point. In the example, an underscore character is used to indicate the presence of a filler space. The underscores are not part of the actual string.

format = "%3d %-30s $%7.2f"

Write the Python code that would allow you to draw a circle without giving your program access to any of the other turtle functions.

from turtle import circle

\n

means go to the next line print("Hello,\nIt's me,") Hello, It's me,

\t

means tab

Write an expression whose value is the same as the str associated with s but with all lower caseletters. Thus, if the str associated with s were "McGraw15", the value of the expression would be "mcgraw15".

s.lower()

Given a variable s associated with a str, write an expression whose value is a str that is identical except that all the letters in it are upper-case. Thus, if the str associated with s were "McGraw15", the value of the expression would be "MCGRAW15".

s.upper()

Write an expression that is the concatenation of the str associated with s1 and that associated with the str s2.

s1 + s2

s1 = ""

same as s1=str() creates an empty string

s2 = "Welcome"

same as s2 =string("Welcome")

Associate the variable named text with the empty string.

text=""

max(1,5,2)

5

round (5.4), round (5.5), round(4.5)

5,6,4

round(5.463,2)

5.46

round(5.466,2)

5.47

pow(2,3)

8

3 ** 2

9

Write a String constant consisting of exactly 5 exclamation marks.

" !!!!! "

Write a String constant that is the empty string.

""

Given a variable word that has been assigned a string value, write a string expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the string "(sadly)"

"("+word+")"

Write an expression that is the concatenation of the strings "Hello" and "World".

"Hello" + "World"

Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable's String value. So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

"http://"+address

min(1,5,2)

1

How is the Unicode character set different from the ASCII character set?

16-bit vs 8-bit; allows us to represent any character from any language

abs(-2)

2

sqrt(4.0)

2

ASCII

American Standard Code for Information Interchange

s2 = str("Welcome")

Creates a string Welcome

Associate two String variables named background and selectionColor with the values "white" and "blue" respectively.

background = "white" selectionColor = "blue"

s1 = str()

creates an empty string

Initialize the variable empty to the empty string.

empty=""

An object is....

entity, has an id and a type,

. In a literal string, a backslash \ is called an

escape character.

\u is an

escape sequence

Write an expression whose value is the concatenation of the three str values associated with name1, name2, and name3, separated by commas. So if name1, name2, and name3, were (respectively) "Neville", "Dean", and "Seamus", your expression's value would be "Neville,Dean,Seamus".

name1 + "," + name2 + "," + name3

in Python, all data- including numbers and strings are

objects

Initialize the variable oneSpace, to a string consisting of a single space.

oneSpace=" "

len()

returns the number of characters(length) in the string str = "I'm 26 years old." print = "String length:", len(str) String length: 17 The output for the above code, in which the str string has 17 characters, including spaces, apostrophes, numbers, and a period, would be as follows:

Write an expression that concatenates the String variable suffix onto the end of the String variable prefix .

prefix + suffix

pow(a,b)

returns a^b same as a **b

abs(x)

returns the absolute value for x

max()

returns the character with the highest value (ASCII)

min()

returns the character with the lowest value

round(x , n )

returns the floor value rounded to n digits after the decimal value, so like if its (2.347, 2) u would round it 2 digits

max(x1, x2, ...)

returns the largest among x1,x2...


Related study sets

Excretory & Immune system quizlet

View Set

Mastering Biology Chapter 7 q+a's

View Set

Conic Sections Skills 3 - vertical and horizontal lines

View Set

Cross Sectional Anatomy: The Final Battle

View Set

Brendan Major physical science chapter 21 section 21.1 page 630

View Set

Chapter 9 - Production and operations management

View Set