Scripting Lecture2
Scripting Lecture2
Scripting languages
Lecture 2: Values, variables, strings, and lists
Note:
• Python follows the precedence rules of mathematics
(exponentiation before multiplication/division before
addition/subtraction)
• Parentheses can be used to specify a different order
• A string (str, short for string of characters) is the type used for
text
• Single quotes, double quotes, or three quotation marks for
different usages
In: 3 + '3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In: number = 42
In: text = str(number)
In: text
Out: '42'
In: int(text)
Out: 42
c = (f - 32) * (5/9)
w = 500
s = 25
c = 3200
w = 500
s = 25
c = 3200
total_words = 500
total_sentences = 25
total_characters = 3200
• A string (str, short for string of characters) is the type used for
text
• Reminder: single quotes ('), double quotes ("), or three double
quotation marks (" " ") for different usages
• Reminder:
• A function is a piece of code that can be invoked by name
• It may take input (arguments): len(name)
• It may produce a result: len(name) or an effect:
print('hello', name)
• The notation f(x) means that the function f is applied to the
value x
In: x = 1
In: y = 2
In: z = x + y
In: x = 1
In: y = 2
In: z = x + y
• Lists are sequences that may contain any kind of value as items