Python Basics

 

In this chapter we will discuss about the following concepts

Tokens-  keywords, literals, identifiers, special symbols and operators; 

fundamental data types, expressions, type conversions, handling Input and output in Python.

Tokens-

Smallest unit of program is called token

There are following tokens in python:

  • Keywords
  • Identifiers
  • Literals
  • Operators

Python Tokens


I. Keywords

Keywords are the reserved words of the language, cannot be used as ordinary identifiers or variable.  Following are the List of Python Keywords

•      False    await    else    import    pass

•      None    break    except    in    raise

•      True    class    finally    is    return

•       and    continue    for    lambda    try

•       as    def    from    nonlocal    while

•       assert    del    global    not    with

•       async     elif    if    or     yield

1. False - It represents the Boolean false.   if the given condition is false, then it returns false 

Python code

•       >>> 2>3

Output

•       False

2. True - It represents the Boolean true,

•       if the given condition is true, then it returns "True". 

Python code

•       >>> 3>2

output

•       True 

3. None - null value or void ,0-not null

4. and - It is a logical operator.

Python code

•       >>> 2>3 and 3>2

Output

•       False

Python code

•       >>> 6>3 and 3<4

Output

•       True 

5. If – If is  the conditional statement. If condition is true it will execute set of statements

Example Python code

even = 20

if(even%2 == 0): 

    print("Even") 

output

even

6.else – It returns false statement. It is used with if statement.

Example Python code

a = 31

if(a%2 == 0): 

    print("Even")

else:

    print("odd")

output

odd

7. elif -  checking the multiple conditions is done by elif.

Sample Python code

sal = int(input("Enter the salary:")) 

if(sal>=90000): 

    print("professor") 

elif(sal<90000 and sal>=75000): 

    print("Associate Professor") 

elif(sal<75000 and sal>=50000): 

    print("Assistant Profesor") 

else: 

    print("Teaching Assistant") 

output

Enter the salary:70000

Assistant Profesor


8. del -  the reference of the object is deleted by del statement. Deleted object no longer executed.

Sample Python code-1

rama=50000

krishna =60000 

del rama

print(rama)

print(krishna)

output

Traceback (most recent call last):

  File "C:/Users/UMA/AppData/Local/Programs/Python/Python37-32/uma3.py", line 4, in <module>

    print(rama)

NameError: name 'rama' is not defined

Sample Python code-2

rama=50000

krishna =60000 

del rama

print(krishna)

print(rama)

output

60000

Traceback (most recent call last):

  File "C:/Users/UMA/AppData/Local/Programs/Python/Python37-32/uma3.py", line 6, in <module>

    print(rama)

NameError: name 'rama' is not defined

When I trying to print rama first, it will give error.It will appear in sample code -1.When I trying to print rama first, it will give Krishna value and give an error. It will appear in sample code-2

9.try, except – These statements are used for  handle the run-time errors.

Sample Python code

try: 

   b = 36/0 

except Exception as e: 

   print(e) 

output

division by zero

10.for –It is a loop statement and used to iterate over the sequences.

Sample Python code

a=["rama","krishna","siva"]

for i in a:

    print(i)

output

rama

krishna

siva

11. While – Set of statements are executed until the condition returns false.

Sample python code

a = 0 

while(a<10): 

    print(a) 

    a = a+1

output

0

1

2

3

4

5

6

7

8

9

12. Lambda  - used for developing the anonymous function 

13. return -  It will return the result value or none 



II Identifier

An identifier is a name for entities (variable, class, function..etc) uniquely. It helps to differentiate one entity from another. Key words are not identifiers

1. Identifier and Variable

•   Both are the names allotted by users

•   The identifier is used to identify an entity uniquely in a program at the time of execution

•  Variable is a name given to a memory location, that is used to hold a value.

•  Variable is an identifier but all identifiers are not variable, other kinds of identifiers are function names, class names, structure names, etc.

• All variables are identifiers whereas, all identifiers are not variables.

 2. The rules for identifier are given below.

•   The first character should be an alphabet or underscore ( _ ).

•   Identifier name should not contain any white-space, or special character (example -- @, #, %, ^, &, *).

•   Identifier is not similar to any keyword.

•   Identifier names are case sensitive

•   All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore, or digit (0-9). 

3. Variable

•  Variable is a container for storing data.

•  Variable is a name for memory location. Python variable is also known as an identifier and used to hold value.

•  Example a=10

•  “a” is variable name

4. Declaration of Variable

•   variable is declared automatically  using equal (=) operator to assign value.

•   Example a = 10

•   Here a is name of the variable , “=”is assignment operator and 10 is value for that variable

5.  Type casting of the variable

Specify the data type of a variable using type casting. 

Sample code-1

>>> a=int(23)

>>> print(a)

Output

23

Sample code-2

>>> b=float(23)

>>> print(b)

Output

23.0

Sample code-3

>>> c=str(23)

>>> print(c)

Output

23

6. Type function

Type function is used to know the data type of the variable.

Sample code-1

>>> a=23

>>> print(type(a))

Output

<class 'int'>

Sample code-2

>>> b="uma"

>>> print(type(b))

Output

<class 'str'>

Sample code-3

>>> c=23.56

>>> print(type(c))

Output

<class 'float'>

7. String variable

String variable is represented by using single or double quotes

Sample code-1

a="rama"

>>> print(a)

Output

rama

Sample code-2

>>> b='rama'

>>> print(b)

Output

Rama


8. Print statement

Print statement is used to output variables. “+ “ is used to combine text and variable.

Sample code-1

a =3

print("the number", a)

Output

the number 3

 



III. Literals

Literal

•      Data is assigned to variable or constant

•      Ex: a=10

Constant

•      Type of variable cannot be changed

•      Ex:  pi=3.14

Types of literals

Numeric literals

•      Numeric Literals are immutable

String Literals

•      String literals can be formed by enclosing a text in the quotes

Boolean Literals

•      Boolean literal--the two values: True or False.

Special Literals

•      Python contains one special literal -- None.

Literal Collections (List, Tuples, dictionary, set)

 Special symbols

•      /n - Newline

•      /t- Horizontal tab

•      /r- Carriage return

•      /b- Backspace

•      /f- Form feed

•      /'- Single Quote

•      /"- double quote

•      -Backslash

IV. Python Operators

It is Symbol  for operation between two operands.

•      Arithmetic operators (+,-,*,/, % (reminder), ** (Exponent))

•      Comparison operators(==,!=,<=,>=,<,>)

•      Assignment Operators (=,+=,-=,**=,||=)

•      Logical Operators (&,||,^,~,<<( left shift),>>)

•      Bitwise Operators (and,or,not)

•      Membership Operators (in, not in)

•      Identity Operators

 



Comments

Popular posts from this blog

32 constructor in python

19 sys module in python

29 exception in python