Python Dictionaries

28 Aug  Admin

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: {}.

Example

thisdictionarie =    {
  “name”: “rohan”,
  “age”: “23”,
  “year”: 1996
}
print(thisdictonarie)

Output

{‘name’: ‘rohan’, ‘age’: ’23’, ‘year’: 1996}

Read More

Python Sets

 12 Apr  Admin

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

 Sets are unordered, so you cannot be sure in which order the items will appear.

Example

theset = {“bus”, “car”, “aeroplane”}
print(theset)

Output

{‘aeroplane’, ‘bus’, ‘car’}

Read More

Python Lists

28 Aug admin

Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered and unindexed. No duplicate members.
  • Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.

List

A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

alist = [“america”, “india”, “japan”]
print(alist)
Output- 

['america', 'india', 'japan']

Access Items

You access the list items by referring to the index number:

alist = [“apple”, “banana”, “cherry”]
print(alist[2])

output:-cherry

Read More

Python Tuples

28 Aug  Admin

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.

Example

atuple = (“rabbit”, “lion”, “cow”)
print(atuple)

 Output

(‘rabbit’, ‘lion’, ‘cow’)

Read More

Python Operators

12 Apr Admin

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:

OperatorNameExample 
+Additionx + y 
Subtractionx – y 
*Multiplicationx * y 
/Divisionx / y 
%Modulusx % y 
**Exponentiation- left operand raised to the power of rightx ** y(x to the power y) 
//Floor division- division that results into whole number adjusted to the left in the number linex // y 

Python Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleSame As 
=x = 5x = 5 
+=x += 3x = x + 3 
-=x -= 3x = x – 3 
*=x *= 3x = x * 3 
/=x /= 3x = x / 3 
%=x %= 3x = x % 3 
//=x //= 3x = x // 3 
**=x **= 3x = x ** 3 
&=x &= 3x = x & 3 
|=x |= 3x = x | 3 
^=x ^= 3x = x ^ 3 
>>=x >>= 3x = x >> 3 
<<=x <<= 3x = x << 3

Python Comparison Operators

Comparison operators are used to compare values. It returns either True or False according to the condition.

OperatorNameExample 
>Greater thanx > y 
<Less thanx < y 
==Equal tox == y 
!=Not equal tox != y 
>=Greater than or equal tox >= y 
<=Less than or equal tox <= y

Python Logical Operators

Logical operators are used to combine conditional statements:

OperatorMeaningExample 
and True if both the operands are truex < 5 and  x < 10 
orTrue if either of the operands is truex < 5 or x < 4 
notTrue 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:

OperatorDescriptionExample 
is Returns True if both variables are the same objectx  is y 
is notReturns True if both variables are not the same objectx is not y 

Python  Membership Operators

Membership operators are used to test if a sequence is presented in an object:

OperatorDescriptionExample 
in Returns True if a sequence with the specified value is present in the objectx in y 
not inReturns True if a sequence with the specified value is not present in the objectx not in y

Python  Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorNameDescription
ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
 ^XORSets each bit to 1 if only one of two bits is 1
NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Read More

Python Data Types

28   Admin

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:intfloatcomplex
Sequence Types:listtuplerange
Mapping Type:dict
Set Types:setfrozenset
Boolean Type:bool
Binary Types:bytesbytearraymemoryview

In Python, the data type is set when you assign a value to a variable:

ExampleData Type 
x = “Welcome”str 
x = 14int 
x = 21.5float 
x = 4jcomplex 
x = [“monkey”, “dog”, “cow”]list 
x = (“orange”, “mango”, “apple”)tuple 
x = range(8)range 
x = {“name” : “Mohan”, “age” : 27}dict 
x = {“mango”, “apple”, “banana”}set 
x = frozenset({“sparrow”, “parrot”, “hen”}) frozenset 
x = Truebool 
x = b”Welcome”bytes 
x = bytearray(6)bytearray 
x = memoryview(bytes(6))memoryview 
Read More

Python Variables

28Aug   Admin

A variable can have a short name (like x and y). In Python:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

          Example-     mvar = “Mohan”

Read More

Introduction

27Aug Admin

Python is a programming language.

Python can be used on a server to create web applications.

It is used for:

  • web development (server-side),
  • software development,
  • mathematics,
  • system scripting.
Read More

Positioning Browser Windows

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)

heightThe 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:

window.open(“index.html”, “window_name”,“width=200,height=200,left=”+var_left+“top=”+var_top);

                       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 ).

Read More