0% found this document useful (0 votes)
5 views

Week4-Var. & Datatypes

Uploaded by

rahidsaleemcool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week4-Var. & Datatypes

Uploaded by

rahidsaleemcool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

108443 – Introduction to

Information Technology

Week 4 (Python Basics, Var. & Data Types)

12/28/2024
Lecture Outline

Input & Output functions


Variables & Data Types

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

• print() is a predefined function that can be used to print things on console.

In this case we are printing the string 'Hello World’.

• By predefined we mean that it is built into the Python environment and is

understood by the Python interpreter. This means that the interpreter

knows where to find the definition of the print() function which tells it what

to do when it encounters the print() function.

• You can of course write your own functions and we will be looking at how

to do that later in this course.

• As print() is a function and must be invoked with parentheses.

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..

• Input returns the data in string format.

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

1e2 is a float, but e2 is a variable name !!!!

9 12/28/2024
Variable Assignment
 An assignment statement takes a value and stores it in a
variable
 •Example: x = 5

Variable Assignment Value

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

 Meanings of these keywords would become clear

as you progress in the course

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.

 Companion to / (division) is % (remainder)


 Examples:
 1/2=0
 1 % 2 = 1 (remainder when dividing 1 by 2)
18 12/28/2024
Example: Type float
 Type float represents real numbers
 Float is short for “floating point”
 values: distinguished from integers by decimal points
 In Python a number with a “.” is a float literal (e.g. 2.0)
 Without a decimal a number is an int literal (e.g. 2)
 operations: +, –, *, /, **
 The meaning for floats differs from that for ints
 Example: 1.0/2.0 evaluates to 0.5
 Exponent notation is useful for large (or small) values
 –22.51e6 is –22.51 * 106 or –22510000
 22.51e–6 is 22.51* 10–6 or 0.00002251
19 12/28/2024
Example: Type bool
 Type Boolean or bool represents logical statements
 values: True, False
 Boolean literals are just True and False(have to be capitalized)
 operations: not, and, or
 not b: True if b is false and False if b is true
 b and c:True if both b and c are true; False otherwise
 b or c:True if b is true or c is true; False otherwise
 Boolean Output from comparing int or float values
 Order comparison: i< j i<= j i>= j i> j
 Equality, inequality: i==j i!= j

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

You might also like