Data Types and Operators in Python

Identifiers and reserved words are basic building blocks of any programming language an identifier used to identify a module, function, variable, class, or any other object. So let’s get started with the Identifiers of variables. The simplest use of an Identifier is a name given to a variable. The identifier name starts with a letter A to Z or an underscore. An identifier can be followed by numerals 0 to 9, letters, or an underscore. So we are not allowed to use any punctuations or symbolic characters such as (?!, @, $, %) within identifier as it causes an error so basically python is a case sensitive programming language. Let’s see a list for a few naming conventions.

Naming Conventions
The class name starts with an uppercase letter and all other identifiers with a lowercase letter. This is only a convention to help us understand and maintain code better. Now if an identifier starts with a “Single leading underscore” then it indicates that the identifier is meant to be private 
_Single_leading_underscore:
If it is started with a “Two leading underscore” it indicates a strongly private identifier
__Double_leading_underscore:
“Ending an identifier with two trailing underscores”that means it is a language-defined special name_Trailing_underscore__
So next comes to the “Reserved Words” it should not be used as a ConstantsVariables, or Identifiers. Reserved words should be lowercase letters except some. The reserved words are

 

Run this code for keywords list
import sys
import keyword
print("python keywords: ",keyword.kwlist)

The syntax for Python relies heavily on indentation, blocks of code are denoted by line indentation these indentations are rigidly enforced, The number of spaces in the indentation should be consistent in a block of code. Statements in python generally end with a new line, python, however, uses the line continuation character \ backward slash to denote that the line is continuing. Statements within square bracket [ ] curly bracket { } and parentheses ( ) need not to use the line continuation characters to start a new line. Python allows multiple statements on a single line separated by a semicolon. Print (“Statement1”); print(“Statement2”)

Python allows Single quote ‘word’ Double quote “A sentence” and Triple quotes “""A paragraph””” to denotes string literals, remember to use the same type of quote to start and end the string. Triple quotes are used to extend the string across multiple lines. Also, a # hash sign is considered as a comment by python. The interpreter ignores and takes all the characters as a comment that is used after a hash to the physical line. Python allows command-line arguments which means you can provide input via command line making it interactive.


We are going to move on to data types in python and we already talked about identifier and reserved words so we are back to identifiers or names of variables in this case. In python, variables are reserved memory locations which are used to store values whenever you create variables some space are reserved for that in the memory, The interpreter allocates memory based on the data type of a specified variable, it also decides that it can be stored in the reserved memory so by assigning different data types to variables you can store of “Integers” “Decimals” or “characters”.When you assign a value to a variable the declaration is done automatically you don’t need to have an explicit declaration of a variable in python.
Assign a value to variables by using the equal sign = In an assignment statement to the left of the equal is the name and to the right is the value stored in the variable
Item_qty = 10 #An integer
X = 10
Python also allows you to assign a single value to multiple variables simultaneously var1 = var2 = var3 =10 

A classic example of standard data types is a person's age needs to be stored as a numeric value and then his or her address needs to be stored as alphanumeric characters. Python has five standard data types. They are used to define the operation and storage for each of them so these data types are:
Numbers
String
List
Tuple
Set
Dictionary

There are six basic data types supported in python data types that tell us about what kind of values can be stored in a variable and which operations can be performed on each of them. The python language has an extensive set of operators, they are classified into different categories there is also an order of preference in which order the operators get used in the evaluation of expressions. Python has
Arithmetic Operators to carry out
Addition                 +
Subtraction           -
Multiplication        *
Division                  /
Modulus                 %
Floor division        //
Exponent               **

It has a comparison operators to compares the operands this could to check
Equal to                                 ==
Not equal to                           !=
Greater than                          >
Less than                               <
Greater than or equal to      >=
Less than or equal to          <=

It has a assignment operators combined with standard arithmetic operators eg. a+=b is same as a = a+b

Python also has a Logical Operators such as
and             (True if both operands are true) x and y
or                (True if either of the operands is true) x or y
and not      (True if operand is false) not x

In addition python has Membership Operators to check if something is a member of sequence or not.There are two membership operators
in          (True if value/variable is found in the sequence) 2 in x
not in   (True if value/variable is not found in sequence) 2 not in x

Python has Identity Operators to compare the memory locations of two objects
is            (True if operands are identical)
is not     (True if operands are not identical)


So now we are going to get to know a little more about numbers and string data types. Number data types store numeric values they are immutable data types number objects are created as soon as you assign a value to them so you can use the del statement to deletes the reference to a number x = 1 del x python supports four different number types int for signed integers and long for larger integers, you can also have octal and hexadecimal representation oct (x) hex (x)
Flot is for floating-point real values and complex for storing complex numbers. A complex number consists of an ordered pair of real floating-point numbers and it’s denoted by a + bJ where a is a real part and b is the imaginary part of the complex number.
So python supports Mathematical and Trigonometric functions.


Strings are a set of characters in between quotation marks var1 = “Hello!” python does not directly support character types. Subsets of string can be used by slice operators ([ ] and [ : ]) with the index starting at 0 and -1 at the end
“Var1 [0] :”, var1[0]
“Var2 [1 : 5] :” ,var2[1 : 5]
In python, the + sign is the string concatenation operators str + “Test” and the Asterisk symbol * is the repetition operators str * 2
Next Post Previous Post