SlideShare a Scribd company logo
1 of 12
UNIT-1(Lesson 2)
Comments, Identifiers and Keywords
COMMENTS
A computer program is a collection of instructions or statements.
A Python program is usually composed of multiple statements. Each statement is composed of one
or a combination of the following:
1.Comments
2.Whitespace characters
3.Tokens
In a computer program, a comment is used to mark a section of code as non-executable.
Comments are mainly used for two purposes:
a) To mark a section of source code as non-executable, so that the Python interpreter ignores it.
b) To provide remarks or an explanation on the working of the given section of code in plain
English, so that a fellow programmer can read the comments and understand the code.
Types of Comments with examples
In Python, there are two types of comments:
1. Single-line comment : It starts with # (also known as the hash or pound character) and the content following # till the
end of that line is a comment.
2. Docstring comment : Content enclosed between triple quotes, either ''' or """.
Below code example demonstrates the usage of comments:
Key points in relation to comments:
1. The # character must be specified at beginning of a comment line.
2. Comments do not nest. Meaning # has no special meaning inside a comment line which starts with #.
3. One cannot write comments inside string literals which are enclosed between single-quotes or double-quotes.
The # character inside a string literal is treated as part of the string's content.
4. In a comment anything written after # in a particular line is ignored by the interpreter. Meaning it does not form part of
the executable code in the program.
Practice Question:
1. Make the following changes in the below code :
Remove the comment on the line which prints "I am a Python Guru"
Add a comment on the line which prints "Python is not cool"
Python provides a way to specify documentation comments, called docstrings. A docstring is a string
literal (plain text usually in English) provided in the source code to document what a particular segment
of code does.
 Python allows the use of both """triple-double-quotes""" or '''triple-single-quotes''' to create docstrings. However,
the Python coding conventions specification recommends us to use """triple-double-quotes""" for consistency.
The main purpose of docstrings in Python is to provide information on what a particular Python object does and
not how it does
According to the Python coding conventions, the docstring should always begin with a capital letter and end with
a period (.)
Example:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Note: docstrings inside modules, classes, functions, members, method definitions, etc. will be
done later
Identifier
An identifier is a name used to identify a variable, function, class, module, or object.
In Python, an identifier is like a noun in English.
Identifier helps in differentiating one entity from the other. For example, name and age which speak of two different aspects are called identifiers.
Python is a case-sensitive programming language. Meaning, Age and age are two different identifiers in Python.
Rules:
1) Identifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore ( _ ).
Examples:
myClass, var_1, print_this_to_screen, _number are valid Python identifiers.
2) An identifier can start with an alphabet or an underscore (_), but not with a digit.
Examples:
1_variable is invalid, but variable_1 is perfectly fine.
3) Keywords cannot be used as identifiers. (Keywords are reserved words in Python which have a special meaning).
Examples:
def, and, not, for, while, if, else etc.
4) Special symbols like !, @, #, $, % etc. are not allowed in identifiers. Only one special symbol underscore (_) is allowed.
Examples:
company#name, $name, email@id are invalid Python identifiers.
5) Identifiers can be of any length.
Which of the following options are correct?
1. Identifiers are used for identifying entities in a program.
2. We can use any special character like @,#,$ as part of identifiers.
3. 1st_string is a valid identifier.
4. string_1 is valid identifier.
5. Identifiers can be of any length.
A. All are Correct
B. 1,2 and 3 only
C. 1,4 and 5 only
D. 1,3 and 4 only
Which of the following options are correct?
1. Identifiers are used for identifying entities in a program.
2. We can use any special character like @,#,$ as part of identifiers.
3. 1st_string is a valid identifier.
4. string_1 is valid identifier.
5. Identifiers can be of any length.
A. All are Correct
B. 1,2 and 3 only
C. 1,4 and 5 only
D. 1,3 and 4 only
Keywords
Every programming language usually has a set of words know as keywords.
These are reserved words with special meaning and purpose. They are used only for the intended
purpose.
Note : We cannot use a keyword as a variable name, function name or as any other identifier name.
Python 2 has 32 keywords while Python 3.5 has 33 keywords. An extra keyword called nonlocal was
added in Python 3.5. 2 more keywords were added in Python 3.7 making it to 35
Latest version is also having 35 keywords
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Python provides a way to print the list of keywords in its current version.
import keyword # This statement is used to import keyword module.
print(keyword.kwlist) # kwlist contains all the keywords of Python
More on keywords
To check whether a given word is a python keyword or not, we use a built-in function iskeyword().
This function returns a boolean value, if the given word is keyword then it returns True as output
otherwise returns False.
Let us consider a few examples:
Program - 1:
import keyword # We have to import keyword module
print(keyword.iskeyword('and')) # Here 'and' is a keyword so it prints True as output
Output: True
Program - 2:
import keyword # We have to import keyword module
print(keyword.iskeyword('python')) # Here 'python' is not a keyword so it prints False as output
Output: False
Which of the following options are correct?
1. Python version 3.5 has 33 keywords.
2. true is a valid keyword in Python.
3. The keyword nonlocal does not exist in Python 2.
4. Interpreter raises an error when you try to use keyword as a name of an entity.
5. A programmer can easily modify the keywords.
A. All options are correct
B. 1,2,3 and 4 only
C. 3 and 4 only
D. 1,3 and 4 only
Which of the following options are correct?
1. Python version 3.5 has 33 keywords.
2. true is a valid keyword in Python.
3. The keyword nonlocal does not exist in Python 2.
4. Interpreter raises an error when you try to use keyword as a name of an entity.
5. A programmer can easily modify the keywords.
A. All options are correct
B. 1,2,3 and 4 only
C. 3 and 4 only
D. 1,3 and 4 only

More Related Content

Similar to UNIT1Lesson 2.pptx

Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotesSowri Rajan
 
Python - Module 1.ppt
Python - Module 1.pptPython - Module 1.ppt
Python - Module 1.pptjaba kumar
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| FundamentalsMohd Sajjad
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiersNilimesh Halder
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of pythonBijuAugustian
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and commentsNilimesh Halder
 
Python Tutorial Questions part-1
Python Tutorial Questions part-1Python Tutorial Questions part-1
Python Tutorial Questions part-1Srinimf-Slides
 
python full notes data types string and tuple
python full notes data types string and tuplepython full notes data types string and tuple
python full notes data types string and tupleSukhpreetSingh519414
 
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHBhavsingh Maloth
 
Basic concepts of python
Basic concepts of pythonBasic concepts of python
Basic concepts of pythondeepalishinkar1
 
Python Fundamentals for the begginers in programming
Python Fundamentals for the begginers in programmingPython Fundamentals for the begginers in programming
Python Fundamentals for the begginers in programmingbsse20142018
 

Similar to UNIT1Lesson 2.pptx (20)

How To Tame Python
How To Tame PythonHow To Tame Python
How To Tame Python
 
Python Session - 2
Python Session - 2Python Session - 2
Python Session - 2
 
Python Data Types
Python Data TypesPython Data Types
Python Data Types
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotes
 
Python - Module 1.ppt
Python - Module 1.pptPython - Module 1.ppt
Python - Module 1.ppt
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Python programming
Python programmingPython programming
Python programming
 
Python Tutorial Questions part-1
Python Tutorial Questions part-1Python Tutorial Questions part-1
Python Tutorial Questions part-1
 
python full notes data types string and tuple
python full notes data types string and tuplepython full notes data types string and tuple
python full notes data types string and tuple
 
Python PPT.pptx
Python PPT.pptxPython PPT.pptx
Python PPT.pptx
 
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VIII BY BHAVSINGH MALOTH
 
Python Guide.pptx
Python Guide.pptxPython Guide.pptx
Python Guide.pptx
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
 
Basic concepts of python
Basic concepts of pythonBasic concepts of python
Basic concepts of python
 
Python Fundamentals for the begginers in programming
Python Fundamentals for the begginers in programmingPython Fundamentals for the begginers in programming
Python Fundamentals for the begginers in programming
 
Python Programming 1.pptx
Python Programming 1.pptxPython Programming 1.pptx
Python Programming 1.pptx
 
INTERNSHIP REPORT.docx
 INTERNSHIP REPORT.docx INTERNSHIP REPORT.docx
INTERNSHIP REPORT.docx
 

More from CheriviralaNikhil

More from CheriviralaNikhil (6)

A323568347_29695_5_2024_Lecture0_INT232.ppt
A323568347_29695_5_2024_Lecture0_INT232.pptA323568347_29695_5_2024_Lecture0_INT232.ppt
A323568347_29695_5_2024_Lecture0_INT232.ppt
 
Introduction to OS.pdf
Introduction to OS.pdfIntroduction to OS.pdf
Introduction to OS.pdf
 
DPPM V.pdf
DPPM V.pdfDPPM V.pdf
DPPM V.pdf
 
Lecture1-2.ppt
Lecture1-2.pptLecture1-2.ppt
Lecture1-2.ppt
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
Data Models.pptx
Data Models.pptxData Models.pptx
Data Models.pptx
 

Recently uploaded

Low Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service Cuttack
Low Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service CuttackLow Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service Cuttack
Low Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service CuttackSuhani Kapoor
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxGry Tina Tinde
 
Zeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectZeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectPriyanshuRawat56
 
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...Suhani Kapoor
 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...Niya Khan
 
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...Suhani Kapoor
 
Internshala Student Partner 6.0 Jadavpur University Certificate
Internshala Student Partner 6.0 Jadavpur University CertificateInternshala Student Partner 6.0 Jadavpur University Certificate
Internshala Student Partner 6.0 Jadavpur University CertificateSoham Mondal
 
Employee of the Month - Samsung Semiconductor India Research
Employee of the Month - Samsung Semiconductor India ResearchEmployee of the Month - Samsung Semiconductor India Research
Employee of the Month - Samsung Semiconductor India ResearchSoham Mondal
 
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...Suhani Kapoor
 
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...Suhani Kapoor
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012sapnasaifi408
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)Soham Mondal
 
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big BoodyDubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boodykojalkojal131
 
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackVIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackSuhani Kapoor
 
Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...shivangimorya083
 
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service CuttackVIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service CuttackSuhani Kapoor
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证obuhobo
 
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...Suhani Kapoor
 
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...Suhani Kapoor
 

Recently uploaded (20)

Low Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service Cuttack
Low Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service CuttackLow Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service Cuttack
Low Rate Call Girls Cuttack Anika 8250192130 Independent Escort Service Cuttack
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptx
 
Zeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectZeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effect
 
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
 
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
 
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
VIP Russian Call Girls in Bhilai Deepika 8250192130 Independent Escort Servic...
 
Internshala Student Partner 6.0 Jadavpur University Certificate
Internshala Student Partner 6.0 Jadavpur University CertificateInternshala Student Partner 6.0 Jadavpur University Certificate
Internshala Student Partner 6.0 Jadavpur University Certificate
 
Employee of the Month - Samsung Semiconductor India Research
Employee of the Month - Samsung Semiconductor India ResearchEmployee of the Month - Samsung Semiconductor India Research
Employee of the Month - Samsung Semiconductor India Research
 
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
 
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)
 
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big BoodyDubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
Dubai Call Girls Demons O525547819 Call Girls IN DUbai Natural Big Boody
 
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackVIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
 
Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ☎✔👌✔ Whatsapp Body to body massage wi...
 
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service CuttackVIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
VIP Call Girls in Cuttack Aarohi 8250192130 Independent Escort Service Cuttack
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
 
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
 
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
 

UNIT1Lesson 2.pptx

  • 2. COMMENTS A computer program is a collection of instructions or statements. A Python program is usually composed of multiple statements. Each statement is composed of one or a combination of the following: 1.Comments 2.Whitespace characters 3.Tokens In a computer program, a comment is used to mark a section of code as non-executable. Comments are mainly used for two purposes: a) To mark a section of source code as non-executable, so that the Python interpreter ignores it. b) To provide remarks or an explanation on the working of the given section of code in plain English, so that a fellow programmer can read the comments and understand the code.
  • 3. Types of Comments with examples In Python, there are two types of comments: 1. Single-line comment : It starts with # (also known as the hash or pound character) and the content following # till the end of that line is a comment. 2. Docstring comment : Content enclosed between triple quotes, either ''' or """. Below code example demonstrates the usage of comments: Key points in relation to comments: 1. The # character must be specified at beginning of a comment line. 2. Comments do not nest. Meaning # has no special meaning inside a comment line which starts with #. 3. One cannot write comments inside string literals which are enclosed between single-quotes or double-quotes. The # character inside a string literal is treated as part of the string's content. 4. In a comment anything written after # in a particular line is ignored by the interpreter. Meaning it does not form part of the executable code in the program.
  • 4. Practice Question: 1. Make the following changes in the below code : Remove the comment on the line which prints "I am a Python Guru" Add a comment on the line which prints "Python is not cool"
  • 5. Python provides a way to specify documentation comments, called docstrings. A docstring is a string literal (plain text usually in English) provided in the source code to document what a particular segment of code does.  Python allows the use of both """triple-double-quotes""" or '''triple-single-quotes''' to create docstrings. However, the Python coding conventions specification recommends us to use """triple-double-quotes""" for consistency. The main purpose of docstrings in Python is to provide information on what a particular Python object does and not how it does According to the Python coding conventions, the docstring should always begin with a capital letter and end with a period (.) Example: """ This is a comment written in more than just one line """ print("Hello, World!") Note: docstrings inside modules, classes, functions, members, method definitions, etc. will be done later
  • 6. Identifier An identifier is a name used to identify a variable, function, class, module, or object. In Python, an identifier is like a noun in English. Identifier helps in differentiating one entity from the other. For example, name and age which speak of two different aspects are called identifiers. Python is a case-sensitive programming language. Meaning, Age and age are two different identifiers in Python. Rules: 1) Identifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore ( _ ). Examples: myClass, var_1, print_this_to_screen, _number are valid Python identifiers. 2) An identifier can start with an alphabet or an underscore (_), but not with a digit. Examples: 1_variable is invalid, but variable_1 is perfectly fine. 3) Keywords cannot be used as identifiers. (Keywords are reserved words in Python which have a special meaning). Examples: def, and, not, for, while, if, else etc. 4) Special symbols like !, @, #, $, % etc. are not allowed in identifiers. Only one special symbol underscore (_) is allowed. Examples: company#name, $name, email@id are invalid Python identifiers. 5) Identifiers can be of any length.
  • 7. Which of the following options are correct? 1. Identifiers are used for identifying entities in a program. 2. We can use any special character like @,#,$ as part of identifiers. 3. 1st_string is a valid identifier. 4. string_1 is valid identifier. 5. Identifiers can be of any length. A. All are Correct B. 1,2 and 3 only C. 1,4 and 5 only D. 1,3 and 4 only
  • 8. Which of the following options are correct? 1. Identifiers are used for identifying entities in a program. 2. We can use any special character like @,#,$ as part of identifiers. 3. 1st_string is a valid identifier. 4. string_1 is valid identifier. 5. Identifiers can be of any length. A. All are Correct B. 1,2 and 3 only C. 1,4 and 5 only D. 1,3 and 4 only
  • 9. Keywords Every programming language usually has a set of words know as keywords. These are reserved words with special meaning and purpose. They are used only for the intended purpose. Note : We cannot use a keyword as a variable name, function name or as any other identifier name. Python 2 has 32 keywords while Python 3.5 has 33 keywords. An extra keyword called nonlocal was added in Python 3.5. 2 more keywords were added in Python 3.7 making it to 35 Latest version is also having 35 keywords ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] Python provides a way to print the list of keywords in its current version. import keyword # This statement is used to import keyword module. print(keyword.kwlist) # kwlist contains all the keywords of Python
  • 10. More on keywords To check whether a given word is a python keyword or not, we use a built-in function iskeyword(). This function returns a boolean value, if the given word is keyword then it returns True as output otherwise returns False. Let us consider a few examples: Program - 1: import keyword # We have to import keyword module print(keyword.iskeyword('and')) # Here 'and' is a keyword so it prints True as output Output: True Program - 2: import keyword # We have to import keyword module print(keyword.iskeyword('python')) # Here 'python' is not a keyword so it prints False as output Output: False
  • 11. Which of the following options are correct? 1. Python version 3.5 has 33 keywords. 2. true is a valid keyword in Python. 3. The keyword nonlocal does not exist in Python 2. 4. Interpreter raises an error when you try to use keyword as a name of an entity. 5. A programmer can easily modify the keywords. A. All options are correct B. 1,2,3 and 4 only C. 3 and 4 only D. 1,3 and 4 only
  • 12. Which of the following options are correct? 1. Python version 3.5 has 33 keywords. 2. true is a valid keyword in Python. 3. The keyword nonlocal does not exist in Python 2. 4. Interpreter raises an error when you try to use keyword as a name of an entity. 5. A programmer can easily modify the keywords. A. All options are correct B. 1,2,3 and 4 only C. 3 and 4 only D. 1,3 and 4 only