A dictionary is a collection which is unordered, changeable and indexed. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.
A tuple is an immutable sequence of Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses (or round bracket), whereas lists use square brackets.
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
Example:
8+6 = 14
Here, + is the operator that performs addition. 8 and 6 are the operands and 5 is the output of the operation.
There are different type of operators use in python:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator
Name
Example
+
Addition
x + y
–
Subtraction
x – y
*
Multiplication
x * y
/
Division
x / y
%
Modulus
x % y
**
Exponentiation- left operand raised to the power of right
x ** y(x to the power y)
//
Floor division- division that results into whole number adjusted to the left in the number line
x // y
Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator
Example
Same As
=
x = 5
x = 5
+=
x += 3
x = x + 3
-=
x -= 3
x = x – 3
*=
x *= 3
x = x * 3
/=
x /= 3
x = x / 3
%=
x %= 3
x = x % 3
//=
x //= 3
x = x // 3
**=
x **= 3
x = x ** 3
&=
x &= 3
x = x & 3
|=
x |= 3
x = x | 3
^=
x ^= 3
x = x ^ 3
>>=
x >>= 3
x = x >> 3
<<=
x <<= 3
x = x << 3
Python Comparison Operators
Comparison operators are used to compare values. It returns either True or False according to the condition.
Operator
Name
Example
>
Greater than
x > y
<
Less than
x < y
==
Equal to
x == y
!=
Not equal to
x != y
>=
Greater than or equal to
x >= y
<=
Less than or equal to
x <= y
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator
Meaning
Example
and
True if both the operands are true
x < 5 and x < 10
or
True if either of the operands is true
x < 5 or x < 4
not
True if operand is false (complements the operand)
not(x < 5 and x < 10)
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Operator
Description
Example
is
Returns True if both variables are the same object
x is y
is not
Returns True if both variables are not the same object
x is not y
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator
Description
Example
in
Returns True if a sequence with the specified value is present in the object
x in y
not in
Returns True if a sequence with the specified value is not present in the object
x not in y
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operator
Name
Description
&
AND
Sets each bit to 1 if both bits are 1
|
OR
Sets each bit to 1 if one of two bits is 1
^
XOR
Sets each bit to 1 if only one of two bits is 1
~
NOT
Inverts all the bits
<<
Zero fill left shift
Shift left by pushing zeros in from the right and let the leftmost bits fall off
>>
Signed right shift
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
Data types are the classification or categorization of data items.Data types represent a kind of value which determines what operations can be performed on that data. Numeric, non-numeric and Boolean (true/false) data are the most used data types.
Python has the following standard or built-in data types:
Text Type:
str
Numeric Types:
int, float, complex
Sequence Types:
list, tuple, range
Mapping Type:
dict
Set Types:
set, frozenset
Boolean Type:
bool
Binary Types:
bytes, bytearray, memoryview
In Python, the data type is set when you assign a value to a variable:
o The screen object provides you with access to properties of the user’s computer display screen within your JavaScript applications.
o Some of the available properties are:
Property
Description
availHeight
The pixel height of the user’s screen minus the toolbar and any other permanent objects (eg the Windows Taskbar)
availWidth
As availHeight, but dealing with horizontal space
colorDepth
The maximum number of colours the user’s screen can display (in bit format, eg 24 bit, 16 bit etc)
height
The true pixel height of the user’s display
width
The true pixel width of the user’s display
o The left and top parameters of the open() method enable you to specify the position of a window on screen by specifying the number of pixels from the left and top of the screen respectively.
o If you need to use a variable to specify the value of a parameter in the open() method, you would do so as follows:
Where var_left and var_top are the appropriate variables.
o Note the use of the + operator to ensure that the third parameter in the open() method remains as one string when variables are added.
o In order to centre the window on the user’s screen, a little simple geometry is required. The centre of the screen is obviously the point found when we take the width of the screen divided by two, and take the height of the screen divided by two. However, if we set the window’s top and left values to these coordinates, the top left corner of the window will be centred, not the window itself.
o In order to centre the window, we need to subtract half of the window’s height from our top value, and half of the window’s width from our left value. A simple script to accomplish this is as follows:
win_width = 200; win_height = 200;
win_left = (screen.availWidth/2)– (win_width/2);
win_top = (screen.availHeight/2)– (win_height/2);
o By using this script, the values of win_left and win_top will be set correctly for any window using win_width and win_height appropriately to be centred on the screen.
Project
o Open your previous project file, and save it under the name chapter_26.html.
o Modify your existing code to ensure that the logo appears centred on the user’s screen. If possible, do not modify your original function by doing anything more than two new functions – get_win_left( width ) and get_win_top( height ).
Recent Comments