PYTHON
Tokens
• Smallest Individual unit in a program is known as Token
• Python has following tokens: – Keywords – Identifiers – Literals – Operators – Punctuators
Keywords – Keywords are the words that have special meaning reserved by programming
language.
– They are reserved for special purpose and cannot be used for normal identifier names.
– E.g. in, if, break, class, and, continue, True, False
Identifiers – Identifiers are fundamental building block of a program.
– They are names given to variable, functions, objects classes etc.
– Python is case sensitive i.e. it treats upper case and lower case letter differently.
– Naming Conventions: • Contain A-Z, a-z, _ • Name should not start with digit • It can start
with alphabet or ( _ ) underscore
Literals – Literals are data items that have fixed value.
– Python allows several kinds of literals:
• String literals • Special Literal None
• Numeric literals • Literal Collections
• Boolean literals
String Types in Python
• Strings are declared in two ways : – Single-line String: enclosed within single or double
quotes. It must terminate in one line.
– Multiline String: text spread over multiple lines in one single string.
• It can be created in two ways
• By adding backslash at end of each line
• By enclosing the text in triple quotation mark
Numeric Literals
• Integer: positive negative numbers without decimal
• Floating Point: Real numbers with decimal points
• Complex Number: are in form a+bj
Boolean literals =• True and False are the two Boolean Literals
Special Literal None= • Special literal None represent absence of value
• None also indicates end of List
Operator
Operators are the symbol, which triggers some computation when applied on operand.
1. Algorithm operator = +,-,*,/,//,**,%
2. Relational Operators = ‘==’ , !=, > , < , <= , >=
3. Logical Operators = and (True), or(False) , not
4. Assignment Operator = =,+=,-=…
5. I dentity Operator = is , is not
6. Membership Operator = in , not in
Variable and Assignments
Python do not have fixed memory locations, when the value changes they change
their location
Ivalue: expression that can come on lhs of an assignment.
Rvalue: expression that can come on rhs of an assignment.
Dynamic Typing
type( ) function is used to determine the type of object
datatype attached with the variable can change during the program.
Static Type = e data type defined for the first time is fixed for the variable.
Output Through print Statement
sep and end argument in print statement.
Immutable Data Type: Integers Float , Boolean , Complex , Strings , Tuples , Sets
Mutable Data Type = Lists , Dictionary
If-else statement = x=10
1. If x>5
2. If x>15 Result: 1 and 3 result
3. If x>6
If-elif-else statement = Result = 1 result
Types of statements in Python
• They are of three types – Empty Statement: Statement which does nothing. In Python
empty statement is a pass statement. – Simple Statement: Any single executable statement
is a simple statement.
– Compound Statement: Group of Statement executed as a unit. It has a header line and a
body.
• Header line: It begins with the keyword and ends with a colon.
• Body: consist of one or more Python statements ,indented inside header line. All
Statement are at same level of indentation.