0% found this document useful (0 votes)
2 views6 pages

Python Turorail 2

The document covers concepts related to valid variable names, string basics, and number basics in Python programming. It includes examples of valid and invalid variable names, string concatenation, and the use of arithmetic operators. Additionally, it provides exercises for practicing these concepts and understanding operator precedence.

Uploaded by

pokharelarpan759
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)
2 views6 pages

Python Turorail 2

The document covers concepts related to valid variable names, string basics, and number basics in Python programming. It includes examples of valid and invalid variable names, string concatenation, and the use of arithmetic operators. Additionally, it provides exercises for practicing these concepts and understanding operator precedence.

Uploaded by

pokharelarpan759
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/ 6

ONCEPTS IN PRACTICE

Valid variable names


5. Which can be used as a variable name?
a. median
b. class
1.3 • Variables 15
c. import
6. Why is the name, 2nd_input, not a valid variable name?
a. contains an underscore
b. starts with a digit
c. is a keyword
7. Which would be a good name for a variable storing a zip code?
a. z
b. var_2
c. zip_code
8. Given the variable name, DogBreed, which improvement conforms to Python's style guide?
a. dog_breed
b. dogBreed
c. dog-breed
TRY IT
Final score
Write a Python computer program that:
• Creates a variable, team1, assigned with the value "Liverpool".
• Creates a variable, team2, assigned with the value "Chelsea".
• Creates a variable score1, assigned with the value 4.
• Creates a variable, score2, assigned with the value 3.
• Prints team1, "versus", and team2 as a single line of output.
• Prints "Final score: ", score1, "to", score2 as a single line of output.
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-3-variables)
1.4 String basics
Learning objectives
By the end of this section you should be able to
• Use the built-in len() function to get a string's length.
• Concatenate string literals and variables using the + operator.
Quote marks
A string is a sequence of characters enclosed by matching single (') or double (") quotes. Ex:
"Happy
birthday!" and '21' are both strings.
To include a single quote (') in a string, enclose the string with matching double quotes ("). Ex:
"Won't this
work?" To include a double quote ("), enclose the string with matching single quotes ('). Ex:
'They said "Try
it!", so I did'.
16 1 • Statements
Access for free at openstax.org
Valid string Invalid string
"17" or '17' 17
"seventeen" or 'seventeen' seventeen
"Where?" or 'Where?' "Where?'
"I hope you aren't sad." 'I hope you aren't sad.'
'The teacher said "Correct!" ' "The teacher said "Correct!" "
Table 1.3 Rules for strings.
CONCEPTS IN PRACTICE
Valid and invalid strings
1. Which of the following is a string?
a. Hello!
b. 29
c. "7 days"
2. Which line of code assigns a string to the variable email?
a. "[email protected]"
b. "email = [email protected]"
c. email = "[email protected]"
3. Which is a valid string?
a. I know you'll answer correctly!
b. 'I know you'll answer correctly!'
c. "I know you'll answer correctly!"
4. Which is a valid string?
a. You say "Please" to be polite
b. "You say "Please" to be polite"
c. 'You say "Please" to be polite'
len() function
A common operation on a string object is to get the string length, or the number of characters in
the string.
The len() function, when called on a string value, returns the string length.
CHECKPOINT
Using len() to get the length of a string
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1.4 • String basics 17
1-4-string-basics)
CONCEPTS IN PRACTICE
Applying len() function to string values
5. What is the return value for len("Hi Ali")?
a. 2
b. 5
c. 6
6. What is the length of an empty string variable ("")?
a. undefined
b. 0
c. 2
7. What is the output of the following code?
number = "12"
number_of_digits = len(number)
print("Number", number, "has", number_of_digits, "digits.")
a. Number 12 has 12 digits.
b. Number 12 has 2 digits.
c. Number 12 has number_of_digits digits.
Concatenation
Concatenation is an operation that combines two or more strings sequentially with the
concatenation operator
(+). Ex: "A" + "part" produces the string "Apart".
CHECKPOINT
Concatenating multiple strings
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-4-string-basics)
CONCEPTS IN PRACTICE
String concatenation
8. Which produces the string "10"?
a. 1 + 0
b. "1 + 0"
c. "1" + "0"
9. Which produces the string "Awake"?
18 1 • Statements
Access for free at openstax.org
a. "wake" + "A"
b. "A + wake"
c. "A" + "wake"
10. A user enters "red" after the following line of code.
color = input("What is your favorite color?")
Which produces the output "Your favorite color is red!"?
a. print("Your favorite color is " + color + !)
b. print("Your favorite color is " + "color" + "!")
c. print("Your favorite color is " + color + "!")
11. Which of the following assigns "one-sided" to the variable holiday?
a. holiday = "one" + "sided"
b. holiday = one-sided
c. holiday = "one-" + "sided"
TRY IT
Name length
Write a program that asks the user to input their first and last name separately. Use the following
prompts
(example input in bold):
What is your first name? Alan
What is your last name? Turing
The program should then output the length of each name. Based on the example input above,
the output
would be:
Your first name is 4 letters long
Your last name is 6 letters long
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-4-string-basics)
TRY IT
Punctuation
Write a Python computer program that:
• Assigns the string "Freda" to a variable, name.
1.4 • String basics 19
• Assigns the string "happy" to a variable, feel.
• Prints the string "Hi Freda!" with a single print() function using the variable name.
• Prints the string "I'm glad you feel happy." with a single print() function using the variable
feel.
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-4-string-basics)
1.5 Number basics
Learning objectives
By the end of this section you should be able to
• Use arithmetic operators to perform calculations.
• Explain the precedence of arithmetic operators.
Numeric data types
Python supports two basic number formats, integer and floating-point. An integer represents a
whole number,
and a floating-point format represents a decimal number. The format a language uses to
represent data is
called a data type. In addition to integer and floating-point types, programming languages
typically have a
string type for representing text.
CHECKPOINT
Integer and floating-point
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-5-number-basics)
CONCEPTS IN PRACTICE
Integers, floats, and strings
Assume that x = 1, y = 2.0, and s = "32".
1. What is the output of the following code?
print(x, type(x))
a. 1 'int'.
b. 1.0 <class 'float'>.
c. 1 <class 'int'>.
2. What is the output of the following code?
print(y, type(y))
a. 2.0 <class 'int'>
b. 2.0 <class 'float'>
c. 2 <class 'int'>
20 1 • Statements
Access for free at openstax.org
3. What is the type of the following value?
"12.0"
a. string
b. int
c. float
Basic arithmetic
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication,
and division.
Four basic arithmetic operators exist in Python:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
CHECKPOINT
Examples of arithmetic operators
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-5-number-basics)
CONCEPTS IN PRACTICE
Applying arithmetic operators
Assume that x = 7, y = 20, and z = 2.
4. Given the following lines of code, what is the output of the code?
c=0
c=x-z
c=c+1
print(c)
a. 1
b. 5
c. 6
5. What is the value of a?
a = 3.5 - 1.5
a. 2
b. 2.0
1.5 • Number basics 21
c. 2.5
6. What is the output of print(x / z)?
a. 3
b. 3.0
c. 3.5
7. What is the output of print(y / z)?
a. 0
b. 10
c. 10.0
8. What is the output of print(z * 1.5)?
a. 2
b. 3
c. 3.0
Operator precedence
When a calculation has multiple operators, each operator is evaluated in order of precedence.
Ex: 1 + 2 * 3
is 7 because multiplication takes precedence over addition. However, (1 + 2) * 3 is 9 because
parentheses
take precedence over multiplication.
Operator Description Example Result
() Parentheses (1 + 2) * 3 9
** Exponentiation 2 ** 4 16
+, - Positive, negative -math.pi -3.14159
*, / Multiplication, division 2 * 3 6
+, - Addition, subtraction

You might also like