Advertisement

Topic -2

 PYTHON FUNDAMENTALS

  Python Character Set :

It is a set of valid characters that a language recognizes.

Letters: A-Z, a-z Digits : 0-9 Special Symbols Whitespace

   TOKENS

Token: Smallest individual unit in a program is           known as token

There are five types of token in python:


1.     Keyword

2.     Identifier

3.     Literal

4.     Operators

5.     Punctuators

  1.     Keyword: Reserved words in the library of a language. There are 33 keywords in      Python.

 

False

class

finally

is

return

break

None

continue

for

lambda

try

except

True

def

from

nonlocal

while

in

and

del

global

not

with

raise

as

elif

if

or

yield

 

assert

else

import

pass

 

 

 All the keywords are in lowercase except 03 keywords (True, False, None).

2.     Identifier: The name given by the user to the entities like variable name, class-name,  function-name, etc.

 Rules for identifiers:

 §  It can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore.

§  It cannot start with a digit.

§  Keywords cannot be used as an identifier.
§  We cannot use special symbols like !, @, #, $, %, + etc. in identifier.
§  _ (underscore) can be used in an identifier.
§  Commas or blank spaces are not allowed within an identifier.

3.     Literal: Literals are the constant value. Literals can be defined as a data that is given in a variable or constant.

A.   Numeric literals: Numeric Literals are immutable.

 eg.

 7,   8.7,  5+9j

B.   String literals:

 String literals can be formed by enclosing a text in the quotes. We can use both single as well as double quotes for a String.

Eg:

 "Ramesh" , '12345'

 Escape sequence characters:

 

\\

Backslash

\’

Single quote

\”

Double quote

\a

ASCII Bell

\b

Backspace

\f

ASCII Formfeed

\n

New line charater

\t

Horizontal tab

C.   Boolean literal: A Boolean literal can have any of the two values: True or False.

D.   Special literals: Python contains one special literal i.e.    None.

 None is used to specify to that field that is not created. It is also used for end of lists in Python.

 E.   Literal Collections: Collections such as tuples, lists and Dictionary are used in Python.

·         int( ) - constructs an integer number from an integer literal, a float literal or a string literal.

 Example:

 x = int(1)   # x will be 1 

y = int(2.8) # y will be 2

z = int("3") # z will be 3

 ·         float( ) - constructs a float number from an integer literal, a float literal or a string literal.

 Example:

 x = float(1)     # x will be 1.0 

y = float(2.8) # y will be 2.8

z = float("3") # z will be 3.0

w = float("4.2") # w will be 4.2

 ·         str( ) - constructs a string from a wide variety of data types, including strings, integer literals and float literals.

 Example:

 x = str("suresh1") # x will be 'suresh1'

y = str(2)        # y will be '2' 

z = str(3.0) # z will be '3.0'

1. ArithmeticOperators:

Arithmetic Operators are used to performing arithmetic operations like addition, multiplication, division etc.

Example:
x = 5
y = 4
print('x + y =', x+y)
print('x - y =', x-y)
print('x * y =', x*y)
print('x / y =', x/y)
print('x // y =', x//y)
print('x ** y =', x**y)
Output
('x + y ='  9)
('x - y ='  1)
('x * y ='  20)
('x / y ='  1)
('x // y ='  1)
('x ** y ='  625)

2. Relational Operators/ComparisonOperator

Example:
x = 101
y = 121
print('x > y is',x>y)
print('x < y is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)
Output
('x > y is', False)
('x < y is', True)
('x == y is', False)
('x != y is', True)
('x >= y is', False)
('x <= y is', True)
___________________________________________________________________________________
< < Previous Topic -1                                                                                      Next Topic - 3 > >