SlideShare a Scribd company logo
1 of 29
Programming
with
Lecture 1:
Data and Expressions:
Literals, Variables and Identifiers
Dr. Hemant Petwal
Python
Fundamentals
Tokens
Statements
Constant
Variables
Indention
Comments
2
Fundamentals of Python
Tokens
3
Tokens
Reserved Words
Identifiers
Literals
Operators
Tokens are the smallest unit of the program.
• In the following example, the parameter values passed to the print
function are all technically called literals
• More precisely, “Bennett University” and “Welcome to Bennett” are called
textual literals, while 3 and 2.3 are called numeric literals
>>> print(“Bennett University")
Bennett University
>>> print(“Welcome to Bennett")
Welcome to Bennett
>>> print(3)
3
>>> print(2.3)
2.3
Literals
4
Literals
String
Numeric
Boolean
Collection
Special
5
Literals
• String Literals:
• A string literal is a sequence of characters surrounded by quotes. We can
use both single, double or triple quotes for a string. And, a character literal
is a single character surrounded by single or double quotes.
• Numeric Literals:
• Numeric Literals are immutable (unchangeable). Numeric literals can
belong to 3 different numerical types Integer, Float, and Complex.
• Boolean Literals:
• A Boolean literal can have any of the two values: True or False.
• Collection literals:
• There are four different literal collections List literals, Tuple literals, Dict
literals, and Set literals.
• Special literals:
• Python contains one special literal i.e. None. We use it to specify to that
field that is not created.
• Python statements are nothing but logical instructions that interpreter
can read and execute. It can be both single and multiline.
There are two categories of statements in Python:
6
Statements
Statements
Assignments
Simple
Value Based
Variable Based
Operation based
Multiple
Expression
• A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
>>> print(x)
2
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment Statements
 x is a variable and 2 is its value
7
• A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
>>> print(x)
2
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment Statements
 x is a variable and 2 is its value
 x can be assigned different values;
hence, it is called a variable
8
• A simple way to view the effect of an assignment is to assume that when
a variable changes, its old value is replaced
>>> x = 2
>>> print(x)
2
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment Statements: What we
Think
2
Before
x = 2.3
2.3
After
x x
9
• Python assignment statements are slightly different from the “variable as
a box” model
• In Python, values may end up anywhere in memory, and variables are used to
refer to them
>>> x = 2
>>> print(x)
2
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment Statements: What
Actually Happen
2
Before
x = 2.3
2
After
x x
2.3
What will
happen to
value 2?
10
• Interestingly, as a Python programmer you do not have to worry about
computer memory getting filled up with old values when new values are
assigned to variables
• Python will automatically clear old
values out of memory in a process
known as garbage collection
Garbage Collection
2
After
x
2.3
X
Memory location
will be automatically
reclaimed by the
garbage collector
11
• Python allows us also to assign multiple values to multiple variables all
at the same time
• This form of assignment might seem strange at first, but it can prove
remarkably useful (e.g., for swapping values)
Simultaneous Assignment
>>> x, y = 2, 3
>>> x
2
>>> y
3
>>>
12
• Suppose you have two variables x and y, and you want to swap their
values (i.e., you want the value stored in x to be in y and vice versa)
Simultaneous Assignment
>>> x = 2
>>> y = 3
>>> x = y
>>> y = x
>>> x
3
>>> y
3
X CANNOT be done with
two simple assignments
13
• Suppose you have two variables x and y, and you want to swap their
values (i.e., you want the value stored in x to be in y and vice versa)
Simultaneous Assignment
>>> x = 2
>>> y = 3
>>> temp = x
>>> x = y
>>> y = temp
>>> x
3
>>> y
2
>>> x,y = y,x

CAN be done with
three simple assignments,
but more efficiently with
simultaneous assignment
Thus far, we have been using
different names for
variables. These names
are technically called
identifiers
14
• Python has some rules about how identifiers can be formed
• Every identifier must begin with a letter or underscore, which may be followed
by any sequence of letters, digits, or underscores
>>> x1 = 10
>>> x2 = 20
>>> y_effect = 1.5
>>> celsius = 32
>>> 2celsius
File "<stdin>", line 1
2celsius
^
SyntaxError: invalid
syntax
Identifiers
15
• Python has some rules about how identifiers can be formed
• Identifiers are case-sensitive
>>> x = 10
>>> X = 5.7
>>> print(x)
10
>>> print(X)
5.7
Identifiers
16
• Python has some rules about how identifiers can be formed
• Some identifiers are part of Python itself (they are called reserved words or
keywords) and cannot be used by programmers as ordinary identifiers
identifiers
Identifiers
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Python Keywords 17
• Python has some rules about how identifiers can be formed
• Some identifiers are part of Python itself (they are called reserved words or
keywords) and cannot be used by programmers as ordinary identifiers
identifiers
Identifiers
>>> for = 4
File "<stdin>", line 1
for = 4
^
SyntaxError: invalid syntax
An example…
18
• You can produce new data (numeric or text) values in your program
using expressions
Expressions
>>> x = 2 + 3
>>> print(x)
5
>>> print(5 * 7)
35
>>> print("5" + "7")
57
 This is an expression that uses the
addition operator
19
• You can produce new data (numeric or text) values in your program
using expressions
Expressions
>>> x = 2 + 3
>>> print(x)
5
>>> print(5 * 7)
35
>>> print("5" + "7")
57
 This is an expression that uses the
addition operator
 This is another expression that uses the
multiplication operator
20
• You can produce new data (numeric or text) values in your program
using expressions
Expressions
>>> x = 2 + 3
>>> print(x)
5
>>> print(5 * 7)
35
>>> print("5" + "7")
57
 This is an expression that uses the
addition operator
 This is another expression that uses the
multiplication operator
 This is yet another expression that uses the
addition operator but to concatenate (or glue)
strings together
21
• You can produce new data (numeric or text) values in your program
using expressions
Expressions
>>> x = 6
>>> y = 2
>>> print(x -
y)
4
>>> print(x/y)
3.0
>>>
print(x//y)
3
>>> print(x*y)
12
>>> print(x**y)
36
>>> print(x%y)
0
>>> print(abs(-x))
6
Yet another
example…
Another
example…
22
Expressions: Summary of Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Float Division
** Exponentiation
abs() Absolute Value
// Integer Division
% Remainder
Python Built-In Numeric Operations
23
• One problem with entering code interactively into a Python shell is
that the definitions are lost when we quit the shell
• If we want to use these definitions again, we have to type them all over again!
• To this end, programs are usually created by typing definitions into a
separate file called a module or script
• This file is saved on disk so that it can be used over and over again
• A Python module file is just a text file with a .py extension, which can
be created using any program for editing text (e.g., notepad or vim)
Script
24
• A special type of software known as a programming environment
simplifies the process of creating modules/programs.
• A programming environment helps programmers write programs and
includes features such as automatic indenting, color highlighting, and
interactive development.
• The standard Python distribution includes a programming environment
called IDLE that you can use for working on the programs of this
course.
Programming Environments and IDLE
25
• Programs are composed of statements that are built from identifiers and
expressions
• Identifiers are names
• They begin with an underscore or letter which can be followed by a combination
of letter, digit, and/or underscore characters
• They are case sensitive
• Expressions are the fragments of a program that produce data
• They can be composed of literals, variables, and operators
Summary
26
• A literal is a representation of a specific value (e.g., 3 is a literal
representing the number three)
• A variable is an identifier that stores a value, which can change (hence,
the name variable)
• Operators are used to form and combine expressions into more complex
expressions (e.g., the expression x + 3 * y combines two expressions
together using the + and * operators)
Summary
27
• In Python, assignment of a value to a variable is done using the equal
sign (i.e., =)
• Using assignments, programs can get inputs from users and manipulate
them internally
• Python allows simultaneous assignments, which are useful for swapping
values of variables
Summary
28
Thank You
29

More Related Content

Similar to Lecture 1- Python.pptxjhhhfdzdfdggcfffff

Python tutorialfeb152012
Python tutorialfeb152012Python tutorialfeb152012
Python tutorialfeb152012
Shani729
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 

Similar to Lecture 1- Python.pptxjhhhfdzdfdggcfffff (20)

An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
 
Python tutorialfeb152012
Python tutorialfeb152012Python tutorialfeb152012
Python tutorialfeb152012
 
PYTHON
PYTHONPYTHON
PYTHON
 
Python programming
Python programmingPython programming
Python programming
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
02python.ppt
02python.ppt02python.ppt
02python.ppt
 
02python.ppt
02python.ppt02python.ppt
02python.ppt
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of Python
 
cpphtp4_PPT_03.ppt
cpphtp4_PPT_03.pptcpphtp4_PPT_03.ppt
cpphtp4_PPT_03.ppt
 
ppt_pspp.pdf
ppt_pspp.pdfppt_pspp.pdf
ppt_pspp.pdf
 
Cpphtp4 ppt 03
Cpphtp4 ppt 03Cpphtp4 ppt 03
Cpphtp4 ppt 03
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python ppt
Python pptPython ppt
Python ppt
 
Python lab basics
Python lab basicsPython lab basics
Python lab basics
 
Python Objects
Python ObjectsPython Objects
Python Objects
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Python basics
Python basicsPython basics
Python basics
 

Recently uploaded

Obat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di Malang
Obat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di MalangObat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di Malang
Obat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di Malang
Obat Aborsi Jakarta Wa 085176963835 Apotek Jual Obat Cytotec Di Jakarta
 
Obat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di Bandung
Obat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di BandungObat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di Bandung
Obat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di Bandung
Obat Aborsi Jakarta Wa 085176963835 Apotek Jual Obat Cytotec Di Jakarta
 
Powerpoint showing results from tik tok metrics
Powerpoint showing results from tik tok metricsPowerpoint showing results from tik tok metrics
Powerpoint showing results from tik tok metrics
CaitlinCummins3
 
obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...
obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...
obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...
yulianti213969
 
Obat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di Surabaya
Obat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di SurabayaObat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di Surabaya
Obat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di Surabaya
Obat Aborsi Jakarta Wa 085176963835 Apotek Jual Obat Cytotec Di Jakarta
 
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
yulianti213969
 
A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...
A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...
A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...
prakheeshc
 

Recently uploaded (20)

Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
 
Elevate Your Online Presence with SEO Services
Elevate Your Online Presence with SEO ServicesElevate Your Online Presence with SEO Services
Elevate Your Online Presence with SEO Services
 
Obat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di Malang
Obat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di MalangObat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di Malang
Obat Aborsi Malang 0851\7696\3835 Jual Obat Cytotec Di Malang
 
Unlocking Growth The Power of Outsourcing for CPA Firms
Unlocking Growth The Power of Outsourcing for CPA FirmsUnlocking Growth The Power of Outsourcing for CPA Firms
Unlocking Growth The Power of Outsourcing for CPA Firms
 
Home Furnishings Ecommerce Platform Short Pitch 2024
Home Furnishings Ecommerce Platform Short Pitch 2024Home Furnishings Ecommerce Platform Short Pitch 2024
Home Furnishings Ecommerce Platform Short Pitch 2024
 
The Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdf
The Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdfThe Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdf
The Vietnam Believer Newsletter_May 13th, 2024_ENVol. 007.pdf
 
Presentation on cross cultural negotiations.
Presentation on cross cultural negotiations.Presentation on cross cultural negotiations.
Presentation on cross cultural negotiations.
 
Navigating Tax Season with Confidence Streamlines CPA Firms
Navigating Tax Season with Confidence Streamlines CPA FirmsNavigating Tax Season with Confidence Streamlines CPA Firms
Navigating Tax Season with Confidence Streamlines CPA Firms
 
First Time Home Buyer's Guide - KM Realty Group LLC
First Time Home Buyer's Guide - KM Realty Group LLCFirst Time Home Buyer's Guide - KM Realty Group LLC
First Time Home Buyer's Guide - KM Realty Group LLC
 
Obat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di Bandung
Obat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di BandungObat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di Bandung
Obat Aborsi Bandung 0851\7696\3835 Jual Obat Cytotec Di Bandung
 
wagamamaLab presentation @MIT 20240509 IRODORI
wagamamaLab presentation @MIT 20240509 IRODORIwagamamaLab presentation @MIT 20240509 IRODORI
wagamamaLab presentation @MIT 20240509 IRODORI
 
Beyond Numbers A Holistic Approach to Forensic Accounting
Beyond Numbers A Holistic Approach to Forensic AccountingBeyond Numbers A Holistic Approach to Forensic Accounting
Beyond Numbers A Holistic Approach to Forensic Accounting
 
Powerpoint showing results from tik tok metrics
Powerpoint showing results from tik tok metricsPowerpoint showing results from tik tok metrics
Powerpoint showing results from tik tok metrics
 
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptxThompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
 
Progress Report - UKG Analyst Summit 2024 - A lot to do - Good Progress1-1.pdf
Progress Report - UKG Analyst Summit 2024 - A lot to do - Good Progress1-1.pdfProgress Report - UKG Analyst Summit 2024 - A lot to do - Good Progress1-1.pdf
Progress Report - UKG Analyst Summit 2024 - A lot to do - Good Progress1-1.pdf
 
obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...
obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...
obat aborsi jakarta wa 081336238223 jual obat aborsi cytotec asli di jakarta9...
 
Obat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di Surabaya
Obat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di SurabayaObat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di Surabaya
Obat Aborsi Surabaya 0851\7696\3835 Jual Obat Cytotec Di Surabaya
 
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptxGoal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
 
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
 
A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...
A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...
A BUSINESS PROPOSAL FOR SLAUGHTER HOUSE WASTE MANAGEMENT IN MYSORE MUNICIPAL ...
 

Lecture 1- Python.pptxjhhhfdzdfdggcfffff

  • 1. Programming with Lecture 1: Data and Expressions: Literals, Variables and Identifiers Dr. Hemant Petwal
  • 4. • In the following example, the parameter values passed to the print function are all technically called literals • More precisely, “Bennett University” and “Welcome to Bennett” are called textual literals, while 3 and 2.3 are called numeric literals >>> print(“Bennett University") Bennett University >>> print(“Welcome to Bennett") Welcome to Bennett >>> print(3) 3 >>> print(2.3) 2.3 Literals 4
  • 5. Literals String Numeric Boolean Collection Special 5 Literals • String Literals: • A string literal is a sequence of characters surrounded by quotes. We can use both single, double or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes. • Numeric Literals: • Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types Integer, Float, and Complex. • Boolean Literals: • A Boolean literal can have any of the two values: True or False. • Collection literals: • There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals. • Special literals: • Python contains one special literal i.e. None. We use it to specify to that field that is not created.
  • 6. • Python statements are nothing but logical instructions that interpreter can read and execute. It can be both single and multiline. There are two categories of statements in Python: 6 Statements Statements Assignments Simple Value Based Variable Based Operation based Multiple Expression
  • 7. • A literal is used to indicate a specific value, which can be assigned to a variable >>> x = 2 >>> print(x) 2 >>> x = 2.3 >>> print(x) 2.3 Simple Assignment Statements  x is a variable and 2 is its value 7
  • 8. • A literal is used to indicate a specific value, which can be assigned to a variable >>> x = 2 >>> print(x) 2 >>> x = 2.3 >>> print(x) 2.3 Simple Assignment Statements  x is a variable and 2 is its value  x can be assigned different values; hence, it is called a variable 8
  • 9. • A simple way to view the effect of an assignment is to assume that when a variable changes, its old value is replaced >>> x = 2 >>> print(x) 2 >>> x = 2.3 >>> print(x) 2.3 Simple Assignment Statements: What we Think 2 Before x = 2.3 2.3 After x x 9
  • 10. • Python assignment statements are slightly different from the “variable as a box” model • In Python, values may end up anywhere in memory, and variables are used to refer to them >>> x = 2 >>> print(x) 2 >>> x = 2.3 >>> print(x) 2.3 Simple Assignment Statements: What Actually Happen 2 Before x = 2.3 2 After x x 2.3 What will happen to value 2? 10
  • 11. • Interestingly, as a Python programmer you do not have to worry about computer memory getting filled up with old values when new values are assigned to variables • Python will automatically clear old values out of memory in a process known as garbage collection Garbage Collection 2 After x 2.3 X Memory location will be automatically reclaimed by the garbage collector 11
  • 12. • Python allows us also to assign multiple values to multiple variables all at the same time • This form of assignment might seem strange at first, but it can prove remarkably useful (e.g., for swapping values) Simultaneous Assignment >>> x, y = 2, 3 >>> x 2 >>> y 3 >>> 12
  • 13. • Suppose you have two variables x and y, and you want to swap their values (i.e., you want the value stored in x to be in y and vice versa) Simultaneous Assignment >>> x = 2 >>> y = 3 >>> x = y >>> y = x >>> x 3 >>> y 3 X CANNOT be done with two simple assignments 13
  • 14. • Suppose you have two variables x and y, and you want to swap their values (i.e., you want the value stored in x to be in y and vice versa) Simultaneous Assignment >>> x = 2 >>> y = 3 >>> temp = x >>> x = y >>> y = temp >>> x 3 >>> y 2 >>> x,y = y,x  CAN be done with three simple assignments, but more efficiently with simultaneous assignment Thus far, we have been using different names for variables. These names are technically called identifiers 14
  • 15. • Python has some rules about how identifiers can be formed • Every identifier must begin with a letter or underscore, which may be followed by any sequence of letters, digits, or underscores >>> x1 = 10 >>> x2 = 20 >>> y_effect = 1.5 >>> celsius = 32 >>> 2celsius File "<stdin>", line 1 2celsius ^ SyntaxError: invalid syntax Identifiers 15
  • 16. • Python has some rules about how identifiers can be formed • Identifiers are case-sensitive >>> x = 10 >>> X = 5.7 >>> print(x) 10 >>> print(X) 5.7 Identifiers 16
  • 17. • Python has some rules about how identifiers can be formed • Some identifiers are part of Python itself (they are called reserved words or keywords) and cannot be used by programmers as ordinary identifiers identifiers Identifiers False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Python Keywords 17
  • 18. • Python has some rules about how identifiers can be formed • Some identifiers are part of Python itself (they are called reserved words or keywords) and cannot be used by programmers as ordinary identifiers identifiers Identifiers >>> for = 4 File "<stdin>", line 1 for = 4 ^ SyntaxError: invalid syntax An example… 18
  • 19. • You can produce new data (numeric or text) values in your program using expressions Expressions >>> x = 2 + 3 >>> print(x) 5 >>> print(5 * 7) 35 >>> print("5" + "7") 57  This is an expression that uses the addition operator 19
  • 20. • You can produce new data (numeric or text) values in your program using expressions Expressions >>> x = 2 + 3 >>> print(x) 5 >>> print(5 * 7) 35 >>> print("5" + "7") 57  This is an expression that uses the addition operator  This is another expression that uses the multiplication operator 20
  • 21. • You can produce new data (numeric or text) values in your program using expressions Expressions >>> x = 2 + 3 >>> print(x) 5 >>> print(5 * 7) 35 >>> print("5" + "7") 57  This is an expression that uses the addition operator  This is another expression that uses the multiplication operator  This is yet another expression that uses the addition operator but to concatenate (or glue) strings together 21
  • 22. • You can produce new data (numeric or text) values in your program using expressions Expressions >>> x = 6 >>> y = 2 >>> print(x - y) 4 >>> print(x/y) 3.0 >>> print(x//y) 3 >>> print(x*y) 12 >>> print(x**y) 36 >>> print(x%y) 0 >>> print(abs(-x)) 6 Yet another example… Another example… 22
  • 23. Expressions: Summary of Operators Operator Operation + Addition - Subtraction * Multiplication / Float Division ** Exponentiation abs() Absolute Value // Integer Division % Remainder Python Built-In Numeric Operations 23
  • 24. • One problem with entering code interactively into a Python shell is that the definitions are lost when we quit the shell • If we want to use these definitions again, we have to type them all over again! • To this end, programs are usually created by typing definitions into a separate file called a module or script • This file is saved on disk so that it can be used over and over again • A Python module file is just a text file with a .py extension, which can be created using any program for editing text (e.g., notepad or vim) Script 24
  • 25. • A special type of software known as a programming environment simplifies the process of creating modules/programs. • A programming environment helps programmers write programs and includes features such as automatic indenting, color highlighting, and interactive development. • The standard Python distribution includes a programming environment called IDLE that you can use for working on the programs of this course. Programming Environments and IDLE 25
  • 26. • Programs are composed of statements that are built from identifiers and expressions • Identifiers are names • They begin with an underscore or letter which can be followed by a combination of letter, digit, and/or underscore characters • They are case sensitive • Expressions are the fragments of a program that produce data • They can be composed of literals, variables, and operators Summary 26
  • 27. • A literal is a representation of a specific value (e.g., 3 is a literal representing the number three) • A variable is an identifier that stores a value, which can change (hence, the name variable) • Operators are used to form and combine expressions into more complex expressions (e.g., the expression x + 3 * y combines two expressions together using the + and * operators) Summary 27
  • 28. • In Python, assignment of a value to a variable is done using the equal sign (i.e., =) • Using assignments, programs can get inputs from users and manipulate them internally • Python allows simultaneous assignments, which are useful for swapping values of variables Summary 28