SlideShare a Scribd company logo
Chapter 2
Dr. Raza Ul Mustafa Khokhar
Dr. Raza Ul Mustafa Khokhar
American University
Computer Science Department - CSC-148
Values and Data Types (1/3)
- A value is one of the fundamental things — like a word or a
number — that a program manipulates.
- The values we have seen so far are 5 (the result when
we added 2 + 3), and "Hello, World!".
- We often refer to these values as objects and we will use
the words value and object interchangeably
→ 2+3 = 5
These objects are classified into different classes, or data types:
4 is an integer, and
"Hello, World!" is a string, so-called because it contains a
string or sequence of letters.
You (and the interpreter) can identify strings because they are
enclosed in quotation marks
Values and Data Types (2/3)
Values and Data Types (3/3)
Floats & Int
- Continuing with our discussion of data types, numbers with
a decimal point belong to a class called float, because these
numbers are represented in a format called floating-point
Strings in python (1/2)
- Strings in Python can be enclosed in either single quotes (') or double quotes ("
- the double quote character), or three of the same separate quote characters ('''
or """).
Strings in python (2/2) - Rules
- Note: Double quoted strings can contain single quotes
inside them, as in "Bruce's beard",
- Single quoted strings can have double quotes inside
them, as in 'The knights who say "Ni!"'.
- Strings enclosed with three occurrences of either quote
symbol are called triple quoted strings. They can contain
either single or double quotes:
Printing in python
Type conversion functions
- Sometimes it is necessary to convert values from one type to another.
- The functions int, float and str will (attempt to) convert their arguments into
types int, float and str respectively. We call these type conversion functions.
See example next page: Think what type of error we have ?
Type conversions example
Variables
- One of the most powerful features of a programming language is the ability to
manipulate variables. A variable is a name that refers to a value.
Flow of variables
Variable names and Keywords
- Variable names can be arbitrarily long. They can
contain both letters and digits, but they have to
begin with a letter or an underscore.
Note: Variable names can never contain spaces.
Invalid variable names
- The underscore character ( _) can also appear in a name. It is often used in
names with multiple words, such as my_name or price_of_tea_in_china.
- There are some situations in which names beginning with an underscore have
special meaning, so a safe rule for beginners is to start all names with a letter.
Python keywords – Not used as a variables
Statements and Expressions
- A statement is an instruction that the Python interpreter can execute.
- We have only seen the assignment statement so far.
- Some other kinds of statements that we’ll see shortly are while statements, for statements, if
statements, and import statements.
- An expression is a combination of values, variables, operators, and calls to
functions. Expressions need to be evaluated. If you ask Python to print an
expression, the interpreter evaluates the expression and displays the result.
Example of expression and statement
Operators and Operands
- Operators are special tokens that represent
computations like addition, multiplication and
division. The values the operator works on are
called operands
Operators and Operands
- The tokens +, -, and *, and the use of parentheses for grouping, mean in
Python what they mean in mathematics.
- The asterisk (*) is the token for multiplication, and ** is the token for
exponentiation. Addition, subtraction, multiplication, and exponentiation all do
what you expect.
Arithmetic Operators:
a = 10
b = 5
# Addition
result_add = a + b # 10 + 5 = 15
# Subtraction
result_sub = a - b # 10 - 5 = 5
# Multiplication
result_mul = a * b # 10 * 5 = 50
# Division
result_div = a / b # 10 / 5 = 2.0
# Modulus (Remainder)
result_mod = a % b # 10 % 5 = 0
# Exponentiation
result_exp = a ** b # 10^5 = 100000
Comparison Operators:
x = 10
y = 20
# Equal to
result_eq = x == y # False
# Not equal to
result_neq = x != y # True
# Greater than
result_gt = x > y # False
# Less than
result_lt = x < y # True
# Greater than or equal to
result_ge = x >= y # False
# Less than or equal to
result_le = x <= y # True
Logical Operators:
p = True
q = False
# Logical AND
result_and = p and q # False
# Logical OR
result_or = p or q # True
# Logical NOT
result_not_p = not p # False
result_not_q = not q # True
Assignment Operators:
x = 10
y = 5
# Addition assignment
x += y # x = x + y = 15
# Subtraction assignment
x -= y # x = x - y = 5
# Multiplication assignment
x *= y # x = x * y = 50
# Division assignment
x /= y # x = x / y = 2.0
Inputs from users
Order of Operations
- Parentheses have the highest precedence and can be used to force an expression to evaluate
in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and
(1+1)**(5-2) is 8. You can also use parentheses to make an expression easier to read, as in
(minute * 100) / 60, even though it doesn’t change the result.
- Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4, and 3*1**3 is 3
and not 27.
- Multiplication and both division operators have the same precedence, which is higher than
addition and subtraction, which also have the same precedence. So 2*3-1 yields 5 rather than
4, and 5-2*2 is 1, not 6.
- Operators with the same precedence (except for **) are evaluated from left-to-right. In
algebra we say they are left-associative. So in the expression 6-3+2, the subtraction happens
first, yielding 3. We then add 2 to get the result 5. If the operations had been evaluated from
right to left, the result would have been 6-(3+2), which is 1.
Example - Order
floor function returns the integer value just lesser than the given rational
value. ceil function returns the integer value just greater than the given
rational value
Reassignment / Updating variables
Thank you. Download slides &
material from Canvas

More Related Content

PPTX
Introduction to Python Values, Variables Data Types Chapter 2
PDF
python2oxhvoudhuSGFsughusgdogusuosFU.pdf
PPT
PythonCourse_02_Expressions.ppt Python introduction turorial for beginner.
PDF
03-Variables, Expressions and Statements (1).pdf
PPTX
Lec2_cont.pptx galgotias University questions
PPTX
Pythonlearn-02-Expressions123AdvanceLevel.pptx
PPTX
Intro to CS Lec03 (1).pptx
PPTX
PPT_1_9102501a-a7a1-493e-818f-cf699918bbf6.pptx
Introduction to Python Values, Variables Data Types Chapter 2
python2oxhvoudhuSGFsughusgdogusuosFU.pdf
PythonCourse_02_Expressions.ppt Python introduction turorial for beginner.
03-Variables, Expressions and Statements (1).pdf
Lec2_cont.pptx galgotias University questions
Pythonlearn-02-Expressions123AdvanceLevel.pptx
Intro to CS Lec03 (1).pptx
PPT_1_9102501a-a7a1-493e-818f-cf699918bbf6.pptx

Similar to Variables in Python & Data Types and Their Values (20)

PPTX
MODULE. .pptx
PDF
Python unit 1 part-2
PPTX
Python Lecture 2
PDF
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
PPTX
unit1 python.pptx
PPTX
Session 4.pptx
PPT
PE1 Module 2.ppt
PPTX
Review old Pygame made using python programming.pptx
PPTX
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
PDF
Python Assignment Statement and Types - Python assignment help
PPTX
03 Variables - Chang.pptx
PPT
02sjjknbjijnuijkjnkggjknbhhbjkjhnilide.ppt
PPTX
An Introduction To Python - Variables, Math
PPTX
2_Data Representation and Expressions (1).pptx
PPTX
PDF
Introduction to Python
PPTX
parts_of_python_programming_language.pptx
PDF
Module1PPT.pdf ,introduction to python programing
PDF
1_Python Basics.pdf
PPTX
modul-python-all.pptx
MODULE. .pptx
Python unit 1 part-2
Python Lecture 2
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
unit1 python.pptx
Session 4.pptx
PE1 Module 2.ppt
Review old Pygame made using python programming.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Python Assignment Statement and Types - Python assignment help
03 Variables - Chang.pptx
02sjjknbjijnuijkjnkggjknbhhbjkjhnilide.ppt
An Introduction To Python - Variables, Math
2_Data Representation and Expressions (1).pptx
Introduction to Python
parts_of_python_programming_language.pptx
Module1PPT.pdf ,introduction to python programing
1_Python Basics.pdf
modul-python-all.pptx
Ad

Recently uploaded (20)

PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
Introduction to machine learning and Linear Models
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
Introduction to Data Science and Data Analysis
PPTX
Database Infoormation System (DBIS).pptx
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PDF
Mega Projects Data Mega Projects Data
PPT
Quality review (1)_presentation of this 21
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
Supervised vs unsupervised machine learning algorithms
Introduction to machine learning and Linear Models
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Fluorescence-microscope_Botany_detailed content
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
Introduction to Data Science and Data Analysis
Database Infoormation System (DBIS).pptx
Qualitative Qantitative and Mixed Methods.pptx
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
STUDY DESIGN details- Lt Col Maksud (21).pptx
Data_Analytics_and_PowerBI_Presentation.pptx
climate analysis of Dhaka ,Banglades.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Mega Projects Data Mega Projects Data
Quality review (1)_presentation of this 21
Introduction-to-Cloud-ComputingFinal.pptx
Ad

Variables in Python & Data Types and Their Values

  • 1. Chapter 2 Dr. Raza Ul Mustafa Khokhar Dr. Raza Ul Mustafa Khokhar American University Computer Science Department - CSC-148
  • 2. Values and Data Types (1/3) - A value is one of the fundamental things — like a word or a number — that a program manipulates. - The values we have seen so far are 5 (the result when we added 2 + 3), and "Hello, World!". - We often refer to these values as objects and we will use the words value and object interchangeably → 2+3 = 5
  • 3. These objects are classified into different classes, or data types: 4 is an integer, and "Hello, World!" is a string, so-called because it contains a string or sequence of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks Values and Data Types (2/3)
  • 4. Values and Data Types (3/3)
  • 5. Floats & Int - Continuing with our discussion of data types, numbers with a decimal point belong to a class called float, because these numbers are represented in a format called floating-point
  • 6. Strings in python (1/2) - Strings in Python can be enclosed in either single quotes (') or double quotes (" - the double quote character), or three of the same separate quote characters (''' or """).
  • 7. Strings in python (2/2) - Rules - Note: Double quoted strings can contain single quotes inside them, as in "Bruce's beard", - Single quoted strings can have double quotes inside them, as in 'The knights who say "Ni!"'. - Strings enclosed with three occurrences of either quote symbol are called triple quoted strings. They can contain either single or double quotes:
  • 9. Type conversion functions - Sometimes it is necessary to convert values from one type to another. - The functions int, float and str will (attempt to) convert their arguments into types int, float and str respectively. We call these type conversion functions. See example next page: Think what type of error we have ?
  • 11. Variables - One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.
  • 13. Variable names and Keywords - Variable names can be arbitrarily long. They can contain both letters and digits, but they have to begin with a letter or an underscore. Note: Variable names can never contain spaces.
  • 14. Invalid variable names - The underscore character ( _) can also appear in a name. It is often used in names with multiple words, such as my_name or price_of_tea_in_china. - There are some situations in which names beginning with an underscore have special meaning, so a safe rule for beginners is to start all names with a letter.
  • 15. Python keywords – Not used as a variables
  • 16. Statements and Expressions - A statement is an instruction that the Python interpreter can execute. - We have only seen the assignment statement so far. - Some other kinds of statements that we’ll see shortly are while statements, for statements, if statements, and import statements. - An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to print an expression, the interpreter evaluates the expression and displays the result.
  • 17. Example of expression and statement
  • 18. Operators and Operands - Operators are special tokens that represent computations like addition, multiplication and division. The values the operator works on are called operands
  • 19. Operators and Operands - The tokens +, -, and *, and the use of parentheses for grouping, mean in Python what they mean in mathematics. - The asterisk (*) is the token for multiplication, and ** is the token for exponentiation. Addition, subtraction, multiplication, and exponentiation all do what you expect.
  • 20. Arithmetic Operators: a = 10 b = 5 # Addition result_add = a + b # 10 + 5 = 15 # Subtraction result_sub = a - b # 10 - 5 = 5 # Multiplication result_mul = a * b # 10 * 5 = 50 # Division result_div = a / b # 10 / 5 = 2.0 # Modulus (Remainder) result_mod = a % b # 10 % 5 = 0 # Exponentiation result_exp = a ** b # 10^5 = 100000
  • 21. Comparison Operators: x = 10 y = 20 # Equal to result_eq = x == y # False # Not equal to result_neq = x != y # True # Greater than result_gt = x > y # False # Less than result_lt = x < y # True # Greater than or equal to result_ge = x >= y # False # Less than or equal to result_le = x <= y # True
  • 22. Logical Operators: p = True q = False # Logical AND result_and = p and q # False # Logical OR result_or = p or q # True # Logical NOT result_not_p = not p # False result_not_q = not q # True
  • 23. Assignment Operators: x = 10 y = 5 # Addition assignment x += y # x = x + y = 15 # Subtraction assignment x -= y # x = x - y = 5 # Multiplication assignment x *= y # x = x * y = 50 # Division assignment x /= y # x = x / y = 2.0
  • 25. Order of Operations - Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even though it doesn’t change the result. - Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27. - Multiplication and both division operators have the same precedence, which is higher than addition and subtraction, which also have the same precedence. So 2*3-1 yields 5 rather than 4, and 5-2*2 is 1, not 6. - Operators with the same precedence (except for **) are evaluated from left-to-right. In algebra we say they are left-associative. So in the expression 6-3+2, the subtraction happens first, yielding 3. We then add 2 to get the result 5. If the operations had been evaluated from right to left, the result would have been 6-(3+2), which is 1.
  • 26. Example - Order floor function returns the integer value just lesser than the given rational value. ceil function returns the integer value just greater than the given rational value
  • 28. Thank you. Download slides & material from Canvas