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

python fundamentals

The document outlines the rules and conventions for writing identifiers in Python, including naming restrictions and case sensitivity. It also explains string literals, including how to use quotes and escape characters, as well as the handling of multi-line strings. Additionally, it provides examples of valid identifiers and demonstrates the use of escape characters for illegal characters in strings.

Uploaded by

shitalsingh5612
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

python fundamentals

The document outlines the rules and conventions for writing identifiers in Python, including naming restrictions and case sensitivity. It also explains string literals, including how to use quotes and escape characters, as well as the handling of multi-line strings. Additionally, it provides examples of valid identifiers and demonstrates the use of escape characters for illegal characters in strings.

Uploaded by

shitalsingh5612
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

 Starting an identifier with a single leading underscore

indicates that the identifier is private.


 If the identifier starts and ends with two underscores, it
is a language-defined special name.
 Identifiers are case-sensitive and must begin with a
letter or an underscore. The first letter of an identifier
cannot be a digit.
A few naming conventions and
Rules for Writing Identifiers in
Python

 Keywords cannot be used as identifier names as they are reserved


words in Python programming language. If you try, it will throw
SyntaxError.
 Python identifiers can contain letters in a small case (a-z), upper case
(A-Z), digits (0-9), and underscore (_).
 There is no limit on the length of the identifier name. But, don’t try to
keep a super long identifier, it will only hurt your credibility as a
programmer.
 Python identifier names are case-sensitive. So, “abc” and “ABC” are
two different identifiers. It’s best to use lowercase letters for identifiers
for uniformity across your programs.
Examples of Valid Identifiers in
Python

ab10c: Contains only letters and numbers


abc_DE: Contains all the valid characters
_: Surprisingly but yes, an underscore is a valid identifier
_abc: Identifier can start with an underscore
(Note: A single underscore at the beginning of an attribute’s name is a
convention that signals to other developers that the attribute is intended
to be private. However, it doesn’t actually prevent access to the
attribute. It’s more of a “gentleman’s agreement” to not access the
attribute directly.)
String literals
String literals

 Strings in python are surrounded by either single quotation marks, or


double quotation marks.
Quotes Inside Quotes
You can use quotes inside a string, as long as they don't match the
quotes surrounding the string:
Example
 print("It's alright")
 print("He is called 'Johnny'")
 print('He is called "Johnny"')
String literals

Multiline Strings - You can assign a multiline string to a variable by using three
quotes:

Example:
You can use three double quotes Or three single quotes

a = """Comments can be used to explain Python code.


Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code."""

Note: in the result, the line breaks are inserted at the same position as in the code.
String literals

For multi-line string with triple quote, while calculating the size, the EOL(End Of Line) is also
counted.
Example:
a=”””Xy
z”””
has size 4.
For multi-line string with single quote and backslash character at the end of line, while calculating
the size, backslash is not counted.
Example:
a=”c\
d\
e”
has size 3
Escape Characters

 To insert characters that are illegal in a string, use an escape


character.
 An escape character is a backslash \ followed by the character you
want to insert.
 An example of an illegal character is a double quote inside a string
that is surrounded by double quotes:
txt = "We are the so-called "Vikings" from the north.“
 The escape character allows you to use double quotes when you
normally would not be allowed:
txt = "We are the so-called \"Vikings\" from the north."
Escape Characters

\' Single Quote txt = 'It\'s alright.'


\\ Backslash txt = "This will insert one \\
(backslash)."
\n New Line txt = "Hello\nWorld!"
\t Tab txt = "Hello\tWorld!"
\ooo Octal value txt = "\110\145\154\154\157“
#outputs Hello
\xhh Hex value txt = "\x48\x65\x6c\x6c\
x6f“#outputs Hello

You might also like