Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

TUTORIAL 2

234128 – Introduction to
Computing with Python

Written and edited by Bronislav Demin


Today
• Basic Components of Code
• Types
• Conversions and Casting
• Exercises

234128 – Introduction to Computing with Python


CONSTANTS, VARIABLES, IDENTIFIERS

234128 – Introduction to Computing with Python


Last Week Reminder

Changes and
Always prints Constant
depends on
“Hello World” user input

234128 – Introduction to Computing with Python 4


Constants
• Constants are any value:
- Numerical: 15.6, 120
- String: “Hello”, ‘bye’
- Boolean: True, False
• Can appear on their own or assigned into
a variable.

234128 – Introduction to Computing with Python 5


Variables
• A Variable is a cell in memory, with a given name (identifier) and
value.
Variable
name

Value

• Before we can use the variable, it must be initialized with a value.


• It is possible to update a variable by assigning a different value:

234128 – Introduction to Computing with Python 6


Assignment
• Assignment is made of two parts:

Left Side: Right Side:


Variable name only. = Some Expression.

• How is Python performing this operation?


1. Evaluating the expression on the right side.
2. Writes the result to the variable.
234128 – Introduction to Computing with Python 7
Assignment
• During assignment, the computer does not “solve an equation”. This
is not Algebra!

WRONG! x + 3 = 7

234128 – Introduction to Computing with Python 8


Assignment
• During assignment, the computer does not “solve an equation”. This
is not Algebra!

WRONG! x + 3 = 7
• The righthand side is evaluated first. Therefore,
• the following statement in Python is legitimate:

234128 – Introduction to Computing with Python 9


Identifiers
• Variable (or function) names are called Identifiers.
• Rules for legal identifiers in Python:
1. Only English letters (lowercase or UPPERCASE), digits or underscore (_)
2. Cannot start with a digit
3. Cannot be a reserved word
• Spyder IDE will mark a reserved word in unique color.
while False def assert import
for True del except from
break None as try raise
Reserved
continue and in with pass
Words
if or is global finally
else not return nonlocal lambda
elif yield class
234128 – Introduction to Computing with Python 10
Identifiers
• Legal identifiers:

temp counter
the_one intro2cs

• Illegal:
1st_street Starts with a digit

do_it! “!” is illegal

finally Reserved word

234128 – Introduction to Computing with Python 11


Identifiers
• The computer doesn’t care what names you choose, but you should.
It is important to choose meaningful names for your variables that will
aid in understanding the function of your program.

• Which example is clearer?

Lowercase and Uppercase letters are not equal. Therefore, x and X are two
different identifiers.
234128 – Introduction to Computing with Python 12
TYPES

234128 – Introduction to Computing with Python 13


Variable Types
• Each variable has a Type.
• Regardless of type, a variable is stored in memory as a sequence of
bits.

• The difference between types signifies how Python translates the


contents of the variable to bits and back.
• These contents can vary (numbers, characters, colors, images,
music….)

Variable Type = the way in which Python interprets the contents of the variable

234128 – Introduction to Computing with Python 14


Variable Types
• In Python, a variable type is set automatically during assignment.

Assignment What the interpreter sees Type name

a = 𝑻𝒓𝒖𝒆 Boolean value (True or False) bool

𝒃 = 𝟑𝟖𝟎 Whole number (Integer) int

𝒄 = 𝟗. 𝟕𝟔 Real Number float

Complex Number complex


𝒅 = 𝟑 + 𝟐𝒋
String str
𝒆 = "𝑯𝒆𝒍𝒍𝒐"
• In Spyder, we can see the different variables and their type under “Variable
Explorer”.
234128 – Introduction to Computing with Python 15
Types: String
What’s
• Python identifies strings by double quotes and single- Wrong?
quotes:
• Double Quotes: my_string = “Hello”
• Single Quotes: my_string = ‘hello’

• Must be consistent:

234128 – Introduction to Computing with Python 16


Type Conversion
• Occasionally, we want to perform calculation on values which are
stored as different types. For example, adding int with float (100 + 2.4).
• To perform such calculation, the type must be converted.
• There are two types of type conversions:
- Automatic – happens without the user’s involvement:

- Implicit – happens by implicit request (also called type casting):

234128 – Introduction to Computing with Python 17


Python Operators
• Addition: +

• Subtraction: -

• Multiplication: *

• Division: /

• Power: **

• Floor division: //

• Modulo: %
234128 – Introduction to Computing with Python 18
Python Operators
• Addition: +

• Subtraction: -

• Multiplication: *

• Division: / Returns float

• Power: **
Return the result
• Floor division: // rounded down

• Modulo: %
234128 – Introduction to Computing with Python 19
Python Operators
• Operators supported by string types:
• Addition: +
• The two operands must be strings.
• Performs concatenation of strings.

• Multiplication: *
• The two operands must be a string and an integer (the order doesn’t
matter).
• Concatenates the string to itself.

234128 – Introduction to Computing with Python 20


Implicit Conversion
• No automatic conversion. The computer doesn’t know which result
you want!

50 or “2525”?!

Implicit
Conversion
(Type Casting)

234128 – Introduction to Computing with Python 21


EXERCISES

234128 – Introduction to Computing with Python 22


Exercise 1
expression type value
5-4
2 // 2
2/2
float(1 // 2)
'python'*3
'python'*3.5
3.5 + int('4.5')
3.5 + float('4.5')
int(1.5 + 1.5)
int(1.5) + int(1.5)

234128 – Introduction to Computing with Python 23


Exercise 1: Solution
expression type value
5-4 int 1
2 // 2 int 1
2/2 float 1.0
float(1 // 2) float 0.0
'python'*3 string 'pythonpythonpython'
'python'*3.5 - -
3.5 + int('4.5') - -
3.5 + float('4.5') float 8.0
int(1.5 + 1.5) int 3
int(1.5) + int(1.5) int 2

234128 – Introduction to Computing with Python 24


Exercise 2:
expression type value
7 // 2
-7 // 2
13.2 // -2
int (7/2)
int (-7/2)
round (7/2)
round (-7/2)
round(float('5.5'))
7%2
10 % 5
234128 – Introduction to Computing with Python 25
Exercise 2: Solution
expression type value
7 // 2 int 3
-7 // 2 int -4
13.2 // -2 float -7.0

int (7/2) int 3

int (-7/2) int -3

round (7/2) int 4


round (-7/2) int -4
round(float('5.5')) int 6
7%2 int 1
10 % 5 int 0

234128 – Introduction to Computing with Python 26


Exercise 2: Solution
expression type value
7 // 2 int 3

Rounded -7 // 2 int -4
down 13.2 // -2 float -7.0

int (7/2) int 3 Removes


decimal
int (-7/2) int -3
point
Returns round (7/2) int 4
closest round (-7/2) int -4
even
number round(float('5.5')) int 6
7%2 int 1
10 % 5 int 0

234128 – Introduction to Computing with Python 27


Exercise 3:
• Write a program that gets a number as input and returns the
following number.

Remember: the function input always returns a string!

234128 – Introduction to Computing with Python 28


Exercise 3: Solution
• Write a program that gets a number as input and returns the
following number:
num = input()
num = int(num)
• Or:
num = int(input())
• Finally, we add:
num = num + 1
print(num)

234128 – Introduction to Computing with Python 29


Comments and Documentation

234128 – Introduction to Computing with Python 30


Exercise 4
• Write a program to calculate length of hypotenuse (use Pythagoras).

234128 – Introduction to Computing with Python 31


Exercise 4: Solution
• Write a program to calculate length of hypotenuse (use Pythagoras).

# Python program to calculate the length of a


hypotenuse
side_a = 3
side_b = 4
# Applying Pythagoras
hypotenuse = (side_a**2+side_b**2)**0.5
# Print the result
print(hypotenuse)

234128 – Introduction to Computing with Python 32


234128 – Introduction to Computing with Python 33

You might also like