3 – Identifiers and Variables

IDENTIFIERS

Identifiers are names given to Variables, Function name, Class name  or any object. 

Conventions in Python Identifiers:

  • An Identifiers may start with an Alphabet, or an Underscore followed by letters
  • Punctuation characters such as @, % and $ are not allowed within indentifiers 
  • Class name should always start with Uppercase
  • An Identifier starting with an underscore is termed to be private
  • An Identifier starting with two underscores is termed as strongly private
  • An Identifier ending  with  two underscores is termed as a language defined special name

KEYWORDS

Keywords are reserved words that can not be used as normal identifier. Python keywords always contain lowercase only. Python Keywords are listed below.

yield with while try
return raise print pass
or not lambda is
in import if global
from for finally exec
except else del def
continue class break assert
and elif    

VARIABLES

Variables are reserved memory space to store values. The size of the memory locations depend on the type of data that is being stored 

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> #Assinging values
>>> a=10
>>> a
10
 
>>> #Multiple Assingment
>>> i=j=k=24
>>> i,j,k
(24, 24, 24)
>>> i,j,k=1,2,3
>>> i,j,k
(1, 2, 3)
>>>

DATA TYPES  

Data type is used to classify the type of the value stored in the variable. Python offers the following data types.

  1. Numbers
  2. String
  3. Tuple
  4. List
  5. Dictionary

NUMBERS

Number data type allows us to store numeric values in it. Numbers may be an interger, float, long and complex.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> #Integer
>>> val=424
>>> type(val)
<type 'int'>
 
>>> #Float
>>> f=34.34
>>> type(f)
<type 'float'>
 
>>> #Long
>>> l=245L
>>> type(l)
<type 'long'>
 
>>> #Complex
>>> c=34.4j
>>> type(c)
<type 'complex'>
>>>

STRING

String is can be formed with single, double or triple quotes. The use of each quotes is given below.

  • String formed using single quotes can contain double quotes within it
  • String formed using double quotes can contain single quotes within it
  • String formed using triple quotes can be used for documents or for multiline string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> #Using Single quotes
>>> s1='Hello everyone "hi"'
>>> print s1
Hello everyone "hi"
 
>>> #Using Double quotes
>>> s2="Hello everyone 'hi'"
>>> print s2
Hello everyone 'hi'
 
>>> #using Triple quotes
>>> s3='''From
Admin,
Mybtechprojects.
To
Visitors.
Content,
Thank you for visiting us.'''
>>> print s3
From
Admin,
Mybtechprojects.
To
Visitors.
Content,
Thank you for visiting us.
>>>

String Operations

  • Slicing operator[] can be used to access subsets of a String
  • +‘ can be used to add two Strings. Some examples are given below
  • *‘ is used to print the String twice.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> s="MyBtechProjects"
>>> s
'MyBtechProjects'
 
>>> #Using Slicing Operator
>>> s[0]
'M'
>>> s[2]
'B'
>>> s[7]
'P'
>>> s[2:]
'BtechProjects'
>>> s[2:7]
'Btech'
 
>>>#Using + Sign
>>> "welcome to "+s
'welcome to MyBtechProjects'
 
>>>#Using - Sign
>>> s*2
'MyBtechProjectsMyBtechProjects'
>>>

TUPLE

Tuple contains array of values separated by comma and enclosed within ( ). Tuple can contain values of differnt types. Tuple is readonly. Once a tuple is created it cannot be modified.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> # Tuple
>>> t=(1,2,3,4,5)
>>> t
(1, 2, 3, 4, 5)
 
>>> t[1]
2
 
>>> t+(6,7,8,9)
(1, 2, 3, 4, 5, 6, 7, 8, 9)
 
>>> t*2
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
 
>>> t[1:4]
(2, 3, 4)
>>> t[2:]
(3, 4, 5)
 
>>> type(t)
<type 'tuple'>
 
>>>#Tring to assign Value
>>> t[2]=2
 
Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    t[2]=2
TypeError: 'tuple' object does not support item assignment
>>>

LIST

List contains array of values separted by comma and enclosed with [ ]. Values in list cab be modified later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> #List
>>> l=[1,2,3,4,5]
>>> l[0]
1
 
>>> l+[6,7,8,9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
 
>>> l*2
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
 
>>> l[2:]
[3, 4, 5]
>>> l[1:4]
[2, 3, 4]
 
>>> type(l)
<type 'list'>
 
>>>#Assinging Values
>>> l[1]=1
>>> l
[1, 1, 3, 4, 5]

DICTIONARY

Dictionary is a collection of key-value pairs. Key is generally of numbers and strings. Value can be of any data type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> dict={}
>>> dict[1]="ONE"
>>> dict[2]="TWO"
>>> dict[3]="THREE"
>>> dict[4]="FOUR"
>>> dict['Five']=5
 
>>> dict
{1: 'ONE', 2: 'TWO', 3: 'THREE', 4: 'FOUR', 'Five': 5}
>>> dict[1]
'ONE'
>>> dict['Five']
5
 
>>> type(dict)
<type 'dict'>
>>>
THANKYOU
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.