Week4-Var. & Datatypes
Week4-Var. & Datatypes
Information Technology
12/28/2024
Lecture Outline
Reference book:
A Beginners Guide to Python 3 Programming by John Hunt
(2nd Edition)
Reference Material
2 12/28/2024 02:37 PM
Output Function (print)
• .py extension
knows where to find the definition of the print() function which tells it what
• You can of course write your own functions and we will be looking at how
3 12/28/2024
Python input() Function
Sometimes we need to get the input from the user in RUN
Time.
In such scenarios, input() function is used.
Example
Ask for the user's name and print it
4 12/28/2024
Cont..
5 12/28/2024
Cont..
6 12/28/2024
Taking multiple inputs from user in Python
7 12/28/2024
Variables and Data Types
8 12/28/2024
Variables
A variable
is a named memory location (box)
contains a value(in the box)
Example x
a
Variable x, with value 5 (of type int)
Variable a with value 20.1 (of type float) 5
20.1
9 12/28/2024
Variable Assignment
An assignment statement takes a value and stores it in a
variable
•Example: x = 5
10 12/28/2024
Assignment Statements
Variable Names
Variable names can be as long, as you like.
They can contain both letters and numbers, but they can’t begin
with a number.
It is legal to use uppercase letters, but it is conventional to use
only lowercase for variables names.
It advisable not to use the name of python keyword as variable
name
11 12/28/2024
Variable Names
Some of keywords of Python 3
12 12/28/2024
Assigning multiple values to multiple variables
13 12/28/2024
Values
Much of Python is storing and computing data
What data values might we want to work with?
Types Examples
Numbers 1, 7, 9, 9.0
Text “Python”
“Programming”
‘Avionics’
Boolean True
False
14 12/28/2024
Expressions & Types
An expression represents something
Python evaluates it (turns it into a value)
Like a calculator
Examples:
2.3 Literal
(3 * 7 + 2) * 0.1 Expression
A set of values, and operations on these values.
Examples of operations: +, -, /, *
The meaning of each operation depends on the type
15 12/28/2024
Data Types
16 12/28/2024
Example: Type int
Type int represents integers
values: …, –3, –2, –1, 0, 1, 2, 3, 4, 5, …
Examples
• 1
• 45
• 43028030
no commas or periods
operations: +, –, *, /, **, %
exponentiation remainder
Add, subtract, multiply, divide
17 12/28/2024
Operations on int’s
Operations on int values must yield an int
Examples:
2+2
• result: 4
4/7
• result: 0
o It takes some time to get used to it.
20 12/28/2024
Example: Type str
Type String or str represents text
values: any sequence of characters
operation(s): + (catenation, or concatenation)
String literal: sequence of characters in quotes
Double quotes: " abcex3$g<&"or "Hello World!"
Single quotes: 'Hello World!'
Concatenation can only apply to strings.
'ab' + 'cd‘ evaluates to 'abcd'
'ab' + 2 produces an error
‘a’*2 results ‘aa’ and so on…
21 12/28/2024
Strings
22 12/28/2024
Strings..
23 12/28/2024
Indexing
24 12/28/2024
Converting Values Between Types
Basic form: type(value)
float(2) converts value 2 to type float (value now 2.0)
int(2.6) converts value 2.6 to type int (value now 2)
Explicit conversion is also called “casting”
Narrow to wide: bool ⇒ int ⇒ float
Widening. Python does automatically if needed
Example: 1/2.0 evaluates to 0.5 (casts 1 to float)
Narrowing. Python never does this automatically
Narrowing conversions cause information to be lost
25 12/28/2024
Operator Precedence
What is the difference between the following?
2*(1+3)
2*1 + 3
Parentheses can be used to make an expression easier to read,
as in (2 *3) / 6, even if it doesn’t
change the result
Operations are performed in a certain order
The acronym PEMDAS is a useful way to remember the rules
26 12/28/2024
Precedence of Python Operators
Exponentiation: ** Precedence goes
Unary operators: + – downwards
Binary arithmetic: * / %
Parentheses highest
Logical ops lowest
Binary arithmetic: + –
Same line = same
Comparisons: < > <= >=
precedence
Equality relations: == !=
Evaluated from left to right
Logical not Example: 1/2*3 is (1/2)*3
Logical and
Logical or
27 12/28/2024
Operators and Type Conversions
Evaluate this Expression Operator Precedence
>>>False + 1 + 3.0 / 3 Exponentiation: **
A. 3 Unary operators: + –
B. 3.0
C. 1.3333 Binary arithmetic: * / %
D. 2
E. 2.0 Binary arithmetic: + –
Comparisons: < > <= >=
Equality relations: == !=
Logical not
Logical and
Logical or
28 12/28/2024
Operators and Type Conversions
Evaluate this Expression Operator Precedence
>>>False + 1 + 3.0 / 3 Exponentiation: **
False + 1 + 3.0 / 3 Unary operators: + –
False + 1 + 1.0 Binary arithmetic: * / %
1 + 1.0 Binary arithmetic: + –
2.0 Comparisons: < > <= >=
Equality relations: == !=
Logical not
Logical and
Logical or
29 12/28/2024
Interactive Mode & Script Mode
Interactive Mode
Interact directly with the interpreter
Good way to get started
Clumsy to work with more than few lines of code
Script Mode
Save code in a file called a script
Python scripts have names that end with .py
30 12/28/2024
Dynamic Typing
Python is a dynamically typed language
Variables can hold values of any type
Variables can hold different types at different times
Use type(x)to find out the type of the value in x
The following is acceptable in Python:
x contains int value
>>> x = 1
x now contains float value
>>> x = x / 2.0
Alternative is a statically typed language(e.g., Java, C, C++
etc.)
Each variable restricted to values of just one type
31 12/28/2024