SlideShare a Scribd company logo
Chapter 2
Dr. Nazim
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 result when we added 2 + 3=5), 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
Ad

More Related Content

Similar to Introduction to Python Values, Variables Data Types Chapter 2 (20)

This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
abhi353063
 
C operators
C operatorsC operators
C operators
GPERI
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docx
mecklenburgstrelitzh
 
java_lect_03-2.ppt
java_lect_03-2.pptjava_lect_03-2.ppt
java_lect_03-2.ppt
kavitamittal18
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
AKANSHAMITTAL2K21AFI
 
Data types, operators and control structures unit-2.pdf
Data types, operators and control structures unit-2.pdfData types, operators and control structures unit-2.pdf
Data types, operators and control structures unit-2.pdf
gurpreetk8199
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
ssuser7f90ae
 
2 1 expressions
2 1  expressions2 1  expressions
2 1 expressions
Ken Kretsch
 
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
venkatapranaykumarGa
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
Inzamam Baig
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1
Prerna Sharma
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
MaheshGour5
 
lecture2 (1).ppt variable s and operators
lecture2 (1).ppt variable s and operatorslecture2 (1).ppt variable s and operators
lecture2 (1).ppt variable s and operators
ChittyAvula
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
MdMahediHasan54
 
Python Objects
Python ObjectsPython Objects
Python Objects
MuhammadBakri13
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
Mohammed Sikander
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and Associativity
Nicole Ynne Estabillo
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
abhi353063
 
C operators
C operatorsC operators
C operators
GPERI
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docx
mecklenburgstrelitzh
 
Data types, operators and control structures unit-2.pdf
Data types, operators and control structures unit-2.pdfData types, operators and control structures unit-2.pdf
Data types, operators and control structures unit-2.pdf
gurpreetk8199
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
ssuser7f90ae
 
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
venkatapranaykumarGa
 
Programming in C Session 1
Programming in C Session 1Programming in C Session 1
Programming in C Session 1
Prerna Sharma
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
MaheshGour5
 
lecture2 (1).ppt variable s and operators
lecture2 (1).ppt variable s and operatorslecture2 (1).ppt variable s and operators
lecture2 (1).ppt variable s and operators
ChittyAvula
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and Associativity
Nicole Ynne Estabillo
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 

Recently uploaded (20)

Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
IPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdf
IPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdfIPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdf
IPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdf
Quiz Club of PSG College of Arts & Science
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Ad

Introduction to Python Values, Variables Data Types Chapter 2

  • 1. Chapter 2 Dr. Nazim 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 result when we added 2 + 3=5), 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

Editor's Notes

  • #27: When the result of floor division (//) is positive, it is as though the fractional portion is truncated off, leaving only the integer portion. When the result is negative, the result is rounded down to the next smallest (greater negative) integer:
  翻译: