Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Python Tokens
Basically, python tokens are the small units of the programming language. Python supports 4 types of Tokenstokens

Keywords :

These are the dedicated words that have special meaning and functions. Moreover, compiler defines these words. Moreover,  it does not allow users to use these words.. Python compiler has the following wordstokens

Identifiers :

Identifiers represent the programmable entities. The programmable entities include user-defined names, variables, modules, and other objects. Moreover, python defines some rules in defining the identifiers. Now let us discuss some of them.An identifier can be a sequence of lower case (or) upper case (or) integers (or) a combination of any.The identifier name should start with the lower case (or) upper case (It must not start with digits)The identifier name should not be a reserved word.Only Underscore (_) is allowed to use as a special character in identifier names.The length of the identifier name should not be more than 79 charactersWould you like to know how Machine learning using python

Literals :

Literals are used to define the data as a variable (or ) constants. Python has 6 literals tokens.tokens

String :

The string is a sequence of characters defined between quotes. (both single and double quotes are applicable to define the string literals.). And these strings perform several operations let us discuss some of them.

SyntaxOperation
Len(String_name)String length
String_name.index(char)Locate the character in the string
String_name.count(char)
String_name[::-1]Reverse the string
String_name.upper()Converts the strings to upper case
String-name.lowerCoverts the string to lower case

Numeric:

These are immutable (unchangeable ) literals. We basically have 3 different numerical types namely integer, float, and complex

Boolean:

This has only two values. i.e true / false.

Collection literals :

A collection literal is a syntactic expression form that evaluates to an aggregate type such as array list (or) Map. Python supports 2 types of collection literal tokenstokens

List Literals :

You can consider the python lists as arrays in C. But the difference between the Arrays and lists is that arrays hold homogeneous data type and lists holds the heterogeneous data types. Basically, this list is the most versatile data type in python. Python literals are separated by a comma in []

Note :

If a comma is not provided between the values, the output does not contain spaces

Example :

List = [‘a’,’b’,’c’]

Print(list)

Output :

[‘a’,’b’,’c’]

The other possible outputs are shown below

CodeOutputExplanation
List[‘a’,’b’,’c’]This would print all the input values
List[0]ALike arrays, the index of the elements starts with 0
List[2]=’d’[‘a’,’b’,’d’]It will update the list at index[2] by d with c
Del list[1][‘a’, ’d’]It would delete the value at index [1]
Len(list)2It  returns the length of the tuple
List*2[‘a’, ’d’][‘a’, ’d’]It prints the output as the number of times the input was given
List[::-1][‘d’,’a’]It would print the result in the reverse order.

Tuples :

Tuples were similar to list. But like list tuples cannot change the values. Beside, tuples are enclosed in parenthesis. whereas lists are enclosed in square brackets.

And as said earlier, these tuples performs all the operations like lists. So I would like to leave the operation for you as a practice. And if you struck up anywhere clarify at python training.

Set :

A set is a well-defined collection of elements.  And the elements in the set are placed in curly braces separated by a comma. In the set every element is unique

Set 1 = {1 , 2,3}

Set 2  = { 1 , 2, 2 , 3}

In the above example the element 2 is taken twice. Now let us discuss the various set operation

tokens

Union:

It combines all the elements in the string. And the union operation performed using the pipe (|) operator tokens.tokens

Ex : A = {1,2,3,4,5,6}

B={3,4,5,6 ,7,8}

A|B = {1,2,3,4,5,6,7,8}

Intersections :

Intersection of A and B returns the common elements in the sets . And the operation is performed using the & operator tokens.tokens

Ex : A = {1,2,3,4,5,6}

B={3,4,5,6 ,7,8}

A &B = {3,4,5,6}

Difference :

Difference of (A-B) returns the elements that are only in A but not in B. Similarly B-A returns only the elements that are only in B but not in A tokens.tokens

Ex : A = {1,2,3,4,5,6}

B={3,4,5,6 ,7,8}

A –B = {1,2}

B-A = {7,8}

Symmetric difference :

It returns the set of elements that are both in A and B Except the common elements tokens.tokens

Ex : A = {1,2,3,4,5,6}

B={3,4,5,6 ,7,8}

A^B ={1,2,7,8}

Dictionaries :

Python dictionaries are the key value pairs that are enclosed in curly braces.dictionaries are separated by the ":"

Dict = {‘name’ : ‘Onlineitguru’, age : 20 }

And these elements accessed as Dict[‘name’]

Output: Onlineitguru

Appending the elements in Dictionaries :

Dict[‘address’]=ameerpet

Output :

‘name’=’onlineitguru’,’age’=20, address’=ameerpet.