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

Working in Python, Identifiers

Uploaded by

ramaraju bathula
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Working in Python, Identifiers

Uploaded by

ramaraju bathula
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Working in Python

If you install Python, there are multiple Python distributions available today.
Default installation available from www.python.org is called CPython
installation and comes with Python interpreter, Python IDLE (Python GUI) and
Pip (Package installer).
There are many other distributions available these days. Anaconda Python
distribution is one such highly recommended distribution that comes
preloaded with many packages and libraries. (e.g., NumPy, SciPy, Panda
libraries etc.)
Many popular IDEs are also available e.g., Spyder IDE, PyCharm IDE etc.
Of these, Spyder IDE is already available as a part of Anaconda Python
distribution.
Script Mode

In script mode, we type Python program in a file and then use the interpreter
to execute the content from the file. Working in interactive mode is convenient
for beginners and for testing small pieces of code, as we can test them
immediately. But for coding more than few lines, we should always save our
code so that we may modify and reuse the code.
To create and run a Python script, we will use following steps in IDLE, if the
script mode is not made available by default with IDLE environment.
1. File>Open OR File>New Window (for creating a new script file)
2. Write the Python code as function i.e. script
3. Save it (^S)
4. Execute it in interactive mode- by using RUN option (^F5)
Otherwise (if script mode is available) start from Step 2
If we write Example 1 in script mode, it will be written in the following way:
Step 1: File> New Window

Step 2:
x=2
y=6
z = x+y
print(z)
Step 3:

Use File > Save or File > Save As - option for saving the file
(By convention all Python program files have names which end with .py)

Step 4:
For execution, press ^F5, and we will go to Python prompt (in other window)
8
Alternatively we can execute the script directly by choosing the RUN option.

Tokens: - The smallest individual unit in a program is known as a Token or


lexical unit.
Python has following tokens:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Puctuators

Keywords: - A keyword is a word having special meaning reserved by


programming language.
Reserved words(35): -
Reserved words associated with literals/ values are reserved literals.

True, False, None (3)


Reserved word associated with some functionality is keyword.--> (32)
'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'

If you see all keywords in Python:


>>>import keyword
>>>keyword.kwlist

Identifier:
A name in Python program is called as Identifier. It can be variable
name, function name or class name.
Rules:
1. The only allowed characters are alphabets (a to z, A to Z), digits (0-9) and
underscore(_).
2. Identifier should not start with digit.
3. Identifiers are case sensitive.
4. Identifier cannot be same as reserved word.
5. There is no length limit for identifiers.

Literals: Literals (often reffered to as constant values) are data items that
have a fixed value. Python allows several kinds of literals:
i. String literals
ii. Numeric literals
iii. Boolean literals
iv. Special literal None
v. Literal collections
String literals:
The text enclosed in quotes forms a string literal in Python.
For example, ‘a’, ‘abc’, “abc” are string literals in Python.
Python allows to nongraphic characters in string values. Nongraphic
characters are those characters that cannot be typed directly from keyboard
i.e., backspace, tabs, carriage return etc. (No character is typed when these
keys are pressed, only some action takes place). These nongraphic
characters are represented using escape sequences. An escape sequence is
represented by a backslash(\) followed by one or more characters.
Escape characters: -
\n  New line
\t  Horizontal tab
\r  Carriage return
\b  Back space
\f  Form feed
\v  Vertical tab
\’  Single quote
\”  Double quote
\\  Back slash

Example : Escape sequence character : start with back slash #


print("hi "Krishna" how r u") SyntaxError: invalid syntax

print("hi \"Krish\" sir")


print("hi \'Rams\' sir")
print("hi \\Hanu\\ sir")
print("hi\Madhavi\tsir)
print("hi\nGhani\nsir")

Numeric Literals:
The numeric literals in Python can belong to any of the following different
numeric types:
Integer literals: Integer literals are whole numbers without any fractional part.
An integer constant must have at least one digit and must not contain any
decimal point. It may contain either +ve or –ve sign.
Ex:
123
-44
Floating point literals: Floating literals are also called real literals.
Real literals are numbers having fractional parts. These may be written
in one of the two forms called fractional form or exponent form.
1. Franctional form consists of signed or unsigned digits including a decimal
point between digits.
Ex:
123.45
3.0
-33.5
2. Exponent form consists of two parts: mantissa and exponent. For instance,
5.8 can be written as 0.58e1=0.58*101, where mantissa part is 0.58 and
exponent part is 1.
Ex:
12.33e3 = 12.33*103=12.33*1000=1233.0

Boolean literals: - A Boolean literal in Python is used to represent one of


the two Boolean values i.e., True or False.
Ex:
T
r
u
e
F
a
ls
e
None literal: -Python has one special literal, which is None. The None
literal is used to indicate absence of value. The None value in Python
means “There is no useful information” or “There’s nothing here”.
Ex:
a=Non
RESERVED WORDS :
In Python some words are reserved to represent some meaning or functionality.
Such types of words are called reserved words.
There are 33 reserved words available in Python.
 True, False, None
 and, or ,not,is
 if, elif, else
 while, for, break, continue, return, in, yield
 try, except, finally, raise, assert
 import, from, as, class, def, pass, global, nonlocal, lambda, del, with

Note:
1. All Reserved words in Python contain only alphabet symbols.
2. Except the following three reserved words, all contain only lower case alphabet
symbols.
 True
 False
 None
Eg:
a= true (wrong)
a=True (correct)

You might also like