0% found this document useful (0 votes)
62 views4 pages

Valid vs Invalid Identifiers in Python

The document contains information about various Python programming concepts like tokens, identifiers, literals, data types, expressions, statements, functions, comments, and more. It provides examples of valid and invalid identifiers in Python. It also describes the different types of literals like string, numeric, boolean and complex literals along with examples.

Uploaded by

bala6927
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

Valid vs Invalid Identifiers in Python

The document contains information about various Python programming concepts like tokens, identifiers, literals, data types, expressions, statements, functions, comments, and more. It provides examples of valid and invalid identifiers in Python. It also describes the different types of literals like string, numeric, boolean and complex literals along with examples.

Uploaded by

bala6927
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

for i in range(1, 11):

if i%2==0:
print(i)

TOKENS IN PYTHON:-
1. Keywords:-for, in, if
2. Identifiers:-i
3. Literals:-1, 11, 2, 0
4. Operators:-%, ==
5. Punctuators:-, and :

FIND OUT VALID OR INVALID IDENTIFIERS:-


Name, name.1, NAME, name-1, name, _name, name1, name_1, 1name, name 1
Valid Identifiers:- Name, NAME, name, _name, name1, name_1
Invalid Identifiers:- name.1, name-1, 1name, name 1

TOKEN:-The smallest individual unit in a program is known as a token or a


lexical unit or a lexical element. Python has following tokens:-
KEYWORDS, IDENTIFIERS, LITERALS, OPERATORS AND PUNCTUATORS

PUNCTUATORS:-Punctuators are symbols that are used in programming language to


organize programming sentence structures and indicate the rhythm and emphasis
of expressions, statements and program structure.
Single quote ' Double quote " Hash # Backslash \ Parentheses () Square Brackets
[] Curly brackets {} At the rate @ Comma , Colon : Dot . Equal =

Identifiers
An identifier is a name given to entities like classes, functions, variables,
objects, lists, dictionaries etc. It helps to differentiate one entity from
another.

Rules for writing identifiers:-


Identifiers can be a combination of letters in lowercase (a to z) or uppercase
(A to Z) or digits (0 to 9) or an underscore _.
Names like myClass, no_1 and MyClass, all are valid example.
An identifier cannot start with a digit. It means 1no is invalid, but no1 is a
valid name.
An identifier must not be a keyword of Python. global = 1 is invalid, but
global1 = 1 is a valid.
We cannot use special symbols like !, @, #, $, % etc. in our identifier.
no# = 1 is a invalid name.
Identifier is a case-sensitive. It means Name and name are not the same.
Multiple words can be joined using an underscore, like no_1.

Literals
Literals are data items that have a fixed value.
Several kinds of literals: - (like String, Numeric, Special and Boolean)
String Literals
String literals: - A string literal is a sequence of characters surrounded by
quotes. We can use single, double and triple quotes for a string.
Some valid string literals are „abc‟, “abc”, „‟‟abc‟‟‟, „123‟, „1-x-0‟, „a‟,
“a” etc.
print(type(„123‟)), print(type(„a‟)
Single line strings: - The string that you create by enclosing text in single
quotes or double quotes in one line is known as single line string.
print(type('python')) print(type("python")) print(type('1-a-0'))
Multiline strings: - The string that you need to write multiple lines is known
as multiline string.
a = 'Hello\ a = “Hello\ a = „„„Hello
world' world” world‟‟‟
print(a) print(a) print(a)
Note: - Don‟t forget to use backslash in single or double quotes.
Here backslash is used to continue the text. It is also known as EOL(End Of
Line).

Numeric Literals
Numeric Literals: - Numeric Literals are immutable (unchangeable). Numeric
literals can belong to 3 different numerical types: Integer, Float and Complex.
Integer Literal: - An integer constant must have at least one digit and must
not contain any decimal point. It may either +ve or -ve sign.
print(bin(65)) # 0b1000001 Binary Literals
print(hex(65)) # 0x41 Hexadecimal Literal
print(oct(65)) # 0o101 Octal Literal
print(ord('A')) # 65 Decimal Literal
print(chr(65)) # A String Literal
print(str(0b1000001)) # 65 Decimal Literal
print(str(0x41)) # 65 Decimal Literal
print(str(0o101)) # 65 Decimal Literal
print(int('0b1000001',2)) # 65 Decimal Literal
print(int('0x41',16)) # 65 Decimal Literal
print(int('0o101',8)) # 65 Decimal Literal

Float Literal: - A real constant in fractional form must have at least one
digit with the decimal point. It may also either +ve or -ve sign.
print(float(65)) # 65.0 fractional form
A real constant in exponent form has two parts: one is mantissa and
another is an exponent. The mantissa is followed by a letter E or e and the
exponent.
print(pow(.3,100)) # 5.153775207320094e-53
print(.3 ** 100) # 5.153775207320094e-53

Complex Literal: Python represents complex numbers in the form A + Bj. Here j
represents imaginary number. The meaning of j is √-1. a = (1+2.56j) +
(1+2.56j)
print(a) # (2+5.12j) print(a.real) # 2.0 print(a.imag) # 5.12
print(complex(65)) # (65+j)
print(complex(65, 2)) # (65+2j)

Special Literal: - Python has one special literal which is used to indicate
absence of value. The name of special literal is None.
def myFunction():
a=1
b=2
c=a+b
#return (c)
x=myFunction() # None
print(x)

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


two Boolean values True or False.
print(bool(0)) # False print(bool(1)) # True

#After finding the calculation, this function will be returned the value.
def myFunction(): #myFunction() is the name of function.
a=10 #the value of a is assigned 10.
b=20 #the value of b is assigned 20.
c=a+b #the calculation of a and b is stored in c.
return (c) #the value will be returned here.
x=myFunction() #the function will be called here.
print(x) #this statement will be printed the value of c.

EXPRESSION:-An expression is any legal combination of symbols that represents a


value. e.g.:- 10, 20, a+b etc.

STATEMENT:-A statement is a programming instruction that does something or some


action takes place. e.g.:- a=10, b=20, c=a+b etc.

COMMENTS:-Comments are the additional readable information which is read by the


programmers but ignored by the Python interpreter.
FULL LINE COMMENTS:-The physical lines beginning with hash sign(#) are the full
line comments.

INLINE COMMENT:-The line starts in the middle of a physical line after python
code is called an inline comment.
DOCSTRINGS:-Comments enclosed in a triple quotes(""") or triple apostrophe(''')
are called docstrings or multiline comment.

FUNCTION:-A function is a code that has a name and it can be reused by


specifying its name in the program where needed.

SUITE:-A group of statements which are part of another statement or a function


are called block or code-block or suite in Python.

INDENTATION:-Python uses indentation to create blocks of code. Statements at


same indentation level are part of same block. It is recommended have a
colon(:) at its end.

WHITESPACE:-You should always have whitespace around operators and after


punctuation but not with parentheses. Python considers these 6 characters as
whitespace: space(' '), new line(\n), horizontal tab(\t), vertical tab(\v),
formfeed(\f) and carriage return(\r).

MULTIPLE ASSIGNMENTS:-Assign same value to multiple variables is called


multiple assignments in single statement.
a = b = c = 10
Assign multiple values to multiple variables is also called multiple
assignments in single statement.
a, b, c = 10, 20, 30

DYNAMIC TYPING:-A variable pointing to a value of a certain type can be made to


point to a value or object of different type this is called dynamic typing.
x = 10 #here x is pointing to an integer value.
print(x)
x = "python" #whereas here x is pointing to a string value.
print(x)

x = 10 #here x is referring to an integer value.


x = "python" #but here x is reassigning to a string value.
print(x)

def myFunction():
a=int(input("Enter the first value:"))
b=int(input("Enter the second value:"))
c=a+b
return(c)
x=myFunction()
print(x)

for row in range(1, 11):


for col in range(1, 11):
product=row*col
if product<11:
print(product,'\t',end='')
else:
print(product,'\t',end='')
print()

You might also like