Lecture 3 - Variables, Datatypes and Operatiors in Python PDF
Lecture 3 - Variables, Datatypes and Operatiors in Python PDF
Operators in Python.
Can be
written as
• Naming conventions
Use of proper naming conventions is a well known best practice. Is a very
common issue where developers use variables like X1, Y1 and forget to
replace them with meaningful ones, causing confusion and making the
code less readable.
• Keep the code simple
The code should always be simple. Complicated logic for achieving simple
tasks is something you want to avoid as the logic one programmer
implemented a requirement may not make perfect sense to another. So,
always keep the code as simple as possible.
4. Tuples
• Tuples are similar to lists, tuples in Python are used to store a set of
elements under a single variable name.
• However, unlike lists, they cannot be changed once created. You can think
of tuples as read-only lists.
• They are used to represent fixed collections of elements, like days in the
week. Elements of a tuple are defined inside the parentheses and
separated by commas. For example,
• myNumbers = (2,4,2,5)
• To access the first element, we use the myNumbers[0] statement.
5. Dictionaries
• Dictionaries are also known as mappings
• They are used to store a set of objects, but they store objects by key instead by
relative position.
• The elements are defined within the curly braces.
• For example, the statement
• myDict = {‘color’ : ‘blue’, ‘size’ : ‘small’}
• This will create a two word dictionary. We can access the elements of this
dictionary by specifying a key.
• To access the value associated with the key color, we would use the following
statement:
• myDict[‘color’].
• There are times you might need to get the current data and time
• For example if you are calculating age of something, audit purposes etc
• To get the current date and time in python, we do the following :
!=
determines whether two values are not equal.
Examples: 1 != 2 returns True, 1 != 1 returns False.
<
determines if the value of the left operand is less than the value of
the right operand.
Examples: 1 < 2 is True, 3 < 2 is False.
• AND
Returns True only if both operands are true. In any other
case, False will be returned.
• OR
Returns True when one or both of the operands are true.
• NOT
Negates the truth value of a single operand.
• AND
Returns True only if both operands are true. In any other
case, False will be returned.
• OR
Returns True when one or both of the operands are true.
• NOT
Negates the truth value of a single operand.
+=
Adds the value found in the right operand to the value found in the left operand.
Example: x = 5, x += 3 results in x = 8.
x = x+3
-=
Subtracts the value of the right operand from the value found in the left operand.
Example: x = 5, x -= 3 results in x = 2.
/=
Divides the value of the left operand by the value of the right operand.
Example: x = 5, x /=3 results in x = 1.667.
x = x/3
%=
Divides the value found in the left operand by the value found in the right
operand. The reminder will be placed in the left operand.
Example: x = 5, x %=3 results in x = 2.
//=
Divides the value found in the left operand by the value of the right
operand. The integer result will be placed in the left operand.
Example: x = 5, x //= 3 results in x = 1.
not in
Evaluates to True if the value in the left operand doesn’t appear in the
sequence found in the right operand.
For example, ‘house’ not in ‘Hello world’ returns True because the
substring house is not present in the string Hello world!.
• is
Returns True if the type of the value in the right operand points to the same
type in the left operand.
For example, type(3) is int evaluates to True because 3 is indeed an integer
number.
• is not
Returns True if the type of the value in the right operand points to a
different type than the value in the left operand.
For example, type(3) is not float evaluates to True because 3 is not a
floating-point value.