Python Basic Syntax
Tokens
The smallest individual unit in a program is known as a token. There are five types of
tokens allowed in Python.
Python Character Set
• Letters: A - Z, a - z
• Digits : 0 - 9
• Special Symbols: space + - * / ** \ ( ) [ ] // = != ==
<> . ‘ “ ‘’’ , ; : % ! & # <= >= @
>>> << >> _
• Whitespaces : Blank space, tab, carriage return, newline, form-
feed
• Other Characters: Python can process all ASCII and uni-code
chars
Python Identifiers
11/16/2023 4
Examples
Variables
Python has no command for declaring a variable
• Must start with a letter or underscore _
• Must consist of letters, numbers, and underscores
• Case Sensitive
Good: spam eggs spam23 _speed
Bad: 23spam #sign var.12
Different: spam Spam SPAM
Rules for Naming Variables
1. Variables names must start with a letter or an underscore
x = 5 # valid
_y = 3 # valid
9x = 10 # starts with numeral
Þ SyntaxError: invalid syntax
$y = False # starts with symbol
=> SyntaxError: invalid syntax
2. The remainder of your variable name may consist of letters,
numbers and underscores.
has_0_in_it = "Still Valid"
3. Names are case sensitive.
x = 9
y = X*5
=> NameError: name 'X‘ is not defined
Knowing the Data Type of Variable
a = 2
print(type(a))
# Output: <type 'int'>
name = 'John Doe'
print(type(name))
# Output: <type 'str'>
q = True
print(type(q))
# Output: <type 'bool'>
Assignment Statements
A single value can be assigned to several variables
simultaneously
a = b = c = 1
print(a, b, c)
# Output: 1 1 1
Python keywords
>>>import keyword
>>>print(keyword.kwlist)
11/16/2023 12
Correct indentation :
Following block generates error :
if 5 > 2:
print("Five is greater than two!")
o/p : File "demo_indentation_test.py", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block
Spaces vs. Tabs
Always use 4 spaces for indentation.
Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 allows mixing tabs and spaces in indentation.
Escape Characters
Escape Description
Sequence
\n Newline. Move the screen cursor to the beginning of
the next line.
\t Horizontal tab. Move the screen cursor to the next tab
stop.
\r Carriage return. Move the screen cursor to the
beginning of the current line; do not advance to the
next line.
\b Backspace. Move the screen cursor back one space.
\a Alert. Sound the system bell.
\\ Backslash. Print a backslash character.
\" Double quote. Print a double quote character.
\' Single quote. Print a single quote character.
Q. Display Good Morning “Ramesh”
Q. Display It’s ok.
String: sequence of characters that is used as data
String literal: string that appears in actual code of a
program
Must be enclosed in single (‘) or double (“) quote marks
String literal can be enclosed in triple quotes (''' or """)
Enclosed string can contain both single and double
quotes and can have multiple lines
Numerical Literals have following types:
1. Integers : Whole Numbers
Examples: 23,100
2. Float : Real Numbers
Examples: 10.5,34.90
3. Complex : Complex Numbers
Examples: 10j , 5+2j
A Boolean Literal in python is used to represent the Boolean
values(True of False).
Example:
X= True
print(x)
True
a= False
print(a)
False
The None literal is used to indicate absence of value.
Example: x=None
print(x)
None