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

Data Type, Variable in Python(2)(2)

Uploaded by

pranav.garg1006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Type, Variable in Python(2)(2)

Uploaded by

pranav.garg1006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Basic Program Structure

Data Type, Variable - PYTHON

Introduction to Computational Thinking 2


Lesson Objectives

At the end of this lesson, you should be able to:


• Describe the following:
- Variables in Python
- Assignment operator in Python
- Arithmetic operators in Python
- Basic numeric data types in Python

• Use variables, assignment operator, arithmetic operators, and basic numeric data
types in coding using Python

Introduction to Computational Thinking 3


Topic Outline

Variables in Python

Assignment Operator in Python

Arithmetic Operators in Python

Basic Numeric Data Types in Python

Introduction to Computational Thinking 4


Variables in Python

Names are used to make the program more readable, so that the “something” is easily understood.
e.g., radiusFloat

# 1. prompt user for the radius


# 2. apply circumference and area formulae
# 3. print the results

import math
radiusString = input("Enter the radius of your circle:")
radiusFloat = float (radiusString)
circumference = 2 * math.pi * radiusFloat
area = math.pi * radiusFloat * radiusFloat

print() # print a line break


print ("The circumference of your circle is:",circumference,\", and the area
is:", area)
More on import, read input, and type conversion

Introduction to Computational Thinking 5


Identifier in Python

Identifier: a name given to an entity in Python


• Helps in differentiating one entity from another

• Name of the entity must be unique to be identified during the execution of the program

Johnson JEFFREY Dorothy


Frank Luke Linda
DOROTHY Maria William Karen

ELIZABETH

MATTHEW
Robert JOHN MARY Kimberly

John
NAMES
Stephen Larry
Joseph Sarah

Ronald
MICHAEL Deborah John
LISA PATRICIA RICHARD Ruth
MARK Sharon Paul Anthony

Introduction to Computational Thinking 6


Rules for Writing Identifiers
• Uppercase and lowercase letters A through Z (26 * 2 = 52)
What can • The underscore, '_’ (1) 52 + 1 + 10 = 63
be used?
• The digits 0 through 9, except for the first character (10)

Syntax Rules in Python


• Must begin with a letter or _ • Upper case and lower case letters are
• 'Ab123' and '_b123' are ok different
• '123ABC' is not allowed 'LengthOfRope' is not 'lengthofrope'

Python is case sensitive


• May contain letters, digits, and underscores
this_is_an_identifier_123 • Can be of any length

• Should not use keywords • Names starting with _ have special meaning
8 9
Introduction to Computational Thinking 7
Keywords

• Special words reserved in Python


• Programmers should not use keywords to name things

Note: Old Python keyword ‘exec’ was removed in Python 3

7
Introduction to Computational Thinking 8
Quick Check

Let’s examine the following variable names, which do you


think are invalid?

int return For


Us$ 2person userName
HALF_WINWIDTH __name__ Phone#

Introduction to Computational Thinking 9


Quick Check: Answer

Let’s examine the following variable names, which do you


think are invalid?

int return For


Us$ 2person userName
HALF_WINWIDTH __name__ Phone#

Allowed Characters: Uppercase and lowercase letters A through Z, the • (Us$, Phone#): $ and # are not allowed;
underscore, '_' and the digits 0 through 9 (except for the first character) • (2person): a digit is not allowed as a first character

Should not use keyword • (return): 'return' is a keyword

Introduction to Computational Thinking 10


A Common Pitfall in Python

john_math_score = 90
Message 1
peter_math_score = 70
mary_math_score = 80 Be careful! Python is
john_eng_score = 60
peter_eng_score = 60
case sensitive!
All English scores are 60
mary_eng_score = 60
Message 2
total = john_math_score + peter_math_score + mary_math_score
average_math = total/3.0 A program, that can
print("average Math score = ", average_math) run doesn’t mean
Total = john_eng_score + peter_eng_score + mary_eng_score
average_eng = total/3.0
that it is correct.
print("average English score = ", average_eng) Logic error

Can we interpret and run this program? Is the result correct?


Hint: Typo

Introduction to Computational Thinking 11


Python Naming Conventions

import math • Both programs work


radiusString = input("Enter the radius of your circle:")
radiusFloat = float (radiusString) • They are different when
circumference = 2 * math.pi * radiusFloat readability counts
area = math.pi * radiusFloat * radiusFloat

vs. • variable names should be in


lowercase, with words separated
import math
by underscores as necessary to
a = input("Enter the radius of your circle:")
improve readability
b = float (a)
c = 2 * math.pi * b e.g. radius_float
d = math.pi * b * b
• mixedCase is allowed
What is c? It is not immediately clear. e.g. radiusFloat

Introduction to Computational Thinking 12


Variable Objects

Operations

• Once a variable is created, we can store, retrieve, or modify the value


associated with the variable name.

• Subsequent assignments can update the associated value.

3.14
Name Value
x = 3.14
x 3.14
X

Name Value 5.16


x = 5.16
x 5.16

Introduction to Computational Thinking 13


Fun Guessing

What do you think is the output of the following Python code?

x = 9
print (x)
x = 7.8
print (x)
x = "welcome"
print (x)

Introduction to Computational Thinking 14


Fun Guessing: Answer

What do you think is the output of the following Python code?

x = 9
print (x)
x = 7.8
print (x)
x = "welcome"
print (x)

9
Answer 7.8
welcome

Introduction to Computational Thinking 15


Data Types

Compared to C and Java, how does Python know the data types?

Python uses Duck-Typing


“When I see a bird that walks like a duck and swims like a duck
and quacks like a duck, I call that bird a duck.” – James Whitcomb Riley

Examples >>> a = 99
Four variables!
>>> b = 99.9
>>> c = '100' What are their data types?
>>> d = True

Introduction to Computational Thinking 16


Data Types (Cont’d)

Type Function >>> x = 9 • Python does not have variable


>>> type (x) declaration, like Java or C, to
<class 'int'> announce or create a variable.
In Python, the
type()function >>> x = 7.8
allows you to know
>>> type(x) • A variable is created by just assigning
<class 'float'> a value to it and the type of the value
the type of a
>>> x = "Welcome" defines the type of the variable.
variable or literal.
>>> type (x)
<class 'str'> • If another value is re-assigned to the
>>> x = 'Python' variable, its type can change.
>>> type (x)
<class 'str'>
>>> type (8.9)
<class 'float'>

Introduction to Computational Thinking 17


Data Types (Cont’d)

String - designated as ‘str’

• It is basically a sequence, typically a Examples


sequence of characters delimited by single
quote ('…') or double quotes ("…") >>> a = "Length"

• First collection type that was discussed >>> b = "1003 welcome“


>>> c = "ewwew sdcd &8 $5##"
• Collection type contains multiple objects
organized as a single object >>> d = 'ewwew sdcd &8 $5##'
More on this later..

Introduction to Computational Thinking 18


Quick Check

What do you think is the output of the following Python code?

total = 4 + 3
sum = total * 2
Total = total + sum
print (total)
print ('Total')

Introduction to Computational Thinking 19


Quick Check: Answer

What do you think is the output of the following Python code?

total = 4 + 3
sum = total * 2
Total = total + sum
print (total)
print ('Total')

7
Answer Total

Introduction to Computational Thinking 20


Scenario 3: Find the Distance Traveled - Recall

Flowchart

start

Read horizonDist

Read vertDist
Preparatory Questions
dist = horizonDist + vertDist
• How many variables should you define? (3)
Display distance between • What is the data type of each variable? (integer)
these two points
• Do you need assignment operator in your program? (Yes)
end • Do you need arithmetic operators in your program? (Yes)

Introduction to Computational Thinking 21


Scenario 3 - Python Codes

Flowchart 1
Python Code Version 3
2
horizon_dist =
horizon_dist = 4
int(input("Read horizonDist"))
start vertical_dist =
vertical_dist = 3int(input("Read vertDist"))
travel_dist = horizon_dist + vertical_dist
Read horizonDist print(travel_dist)
print("distance from A to B is ", travel_dist)

Read vertDist
Output
dist = horizonDist + vertDist
Read horizonDist
distance 7 A to
from 4 B is 7
Display distance between Read vertiDist 3
these two points distance from A to B is 7
print input
end (for displaying data) (for reading data)

Introduction to Computational Thinking 22


Scenario 3 - Python Codes: Comparison

Version 1 Version 2

horizon_dist = 4 horizon_dist = 4
vertical_dist = 3 vertical_dist = 3
travel_dist = horizon_dist + vertical_dist travel_dist = horizon_dist + vertical_dist
print(travel_dist) print("distance from A to B is ", travel_dist)

Output: 7 Output: distance from A to B is 7

Version 3

horizon_dist = int(input("Read horizonDist"))


vertical_dist = int(input("Read vertDist")) Output:
travel_dist = horizon_dist + vertical_dist Read horizonDist 4
print("distance from A to B is ", travel_dist) Read vertDist 7
distance from A to B is 7

Introduction to Computational Thinking 23


Summary

Syntax Rules:
Identifier

Integer
Variables in Operators in Python
Python =, +, -, *, /
Float Data Types Subsequent assignments
can update the associated
value
There are different
Examples of Variables
String data types.
with Operators
value =99
Naming total_price = rice + coffee
Convention area_square = side * side
average = total/number_students

Introduction to Computational Thinking 24


References for Images

Placeholder

Introduction to Computational Thinking 25


Knowledge Concept Check

Introduction to Computational Thinking 26


Information for other school

Hands-on Demonstration

Introduction to Computational Thinking 27

You might also like