Python

Basics of Python – Operators and Operands

In this Page, We are Providing Basics of Python – Operators and Operands. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Basics of Python – Operators and Operands

Operators and operands

An operator is a symbol (such as +, x, etc.) that represents an operation. An operation is an action or •procedure that produces a new value from one or more input values called operands. There are two types of operators: unary and binary. The unary operator operates only on one operand, such as negation. On the other hand, the binary operator operates on two operands, which include addition, subtraction, multiplication, division, exponentiation operators, etc. Consider an expression 3 + 8, here 3 and 8 are called operands, while V is called operator. The operators can also be categorized into:

  • Arithmetic operators.
  • Comparison (or Relational) operators.
  • Assignment operators.
  • Logical operators.
  • Bitwise operators.
  • Membership operators.
  • Identity operators.

Arithematics operators

Table 2-2 enlists the arithmetic operators with a short note on the operators.

Operator

Description

+

Addition operator- Add operands on either side of the operator.

Subtraction operator – Subtract the right-hand operand from the left-hand operand.

*

Multiplication operator – Multiply operands on either side of the operator.

/

Division operator – Divide left-hand operand by right-hand operand.

%

Modulus operator – Divide left-hand operand by right-hand operand and return the remainder.

**

Exponent operator – Perform exponential (power) calculation on operands.

//

Floor Division operator – The division of operands where the result is the quotient in which the digits after the decimal point are removed.

The following example illustrates the use of the above-discussed operators.

>>> a=20 
>>> b=45.0
>>> a+b
65.0
>>> a-b
-25.0
>>> a*b
900.0 
>>> b/a 
2.25 
>>> b%a
5.0
>>> a**b
3.5184372088832e+58 
>>> b//a
2.0

Relational operators

A relational operator is an operator that tests some kind of relation between two operands. Tables 2-3 enlist the relational operators with descriptions.

Operator

Description

==

Check if the values of the two operands are equal.

!=

Check if the values of the two operands are not equal.

<>

Check if the value of two operands is not equal (same as != operator).

>

Check if the value of the left operand is greater than the value of the right operand.

<

Check if the value of the left operand is less than the value of the right operand.

>=

Check if the value of the left operand is greater than or equal to the value of the right operand.

<=

Check if the value of the left operand is less than or equal to the value of the right operand.

The following example illustrates the use of the above-discussed operators.

>>> a,b=20,40
>>> a==b
False 
>>> a!=b 
True
>>> a<>b 
True 
>>> a>b 
False 
>>> a<b 
True
>>> a>=b 
False 
>>> a<=b 
True

Assignment operators

The assignment operator is an operator which is used to bind or rebind names to values. The augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement. An augmented assignment expression like x+=l can be rewritten as x=x+l. Tables 2-4 enlist the assignment operators with descriptions.

Operator

Description

=

Assignment operator- Assigns values from right side operand to left side operand.

+=

Augmented assignment operator- It adds the right-side operand to the left side operand and assigns the result to the left side operand.

-=

Augmented assignment operator- It subtracts the right-side operand from the left side operand and assigns the result to the left side operand.

*=

Augmented assignment operator- It multiplies the right-side operand with the left side operand and assigns the result to the left side operand.

/=

Augmented assignment operator- It divides the left side operand with the right side operand and assigns the result to the left side operand.

%=

Augmented assignment operator- It takes modulus using two operands and assigns the result to left side operand.

* *=

Augmented assignment operator- Performs exponential (power) calculation on operands and assigns value to the left side operand.

//=

Augmented assignment operator- Performs floor division on operators and assigns value to the left side operand.

The following example illustrates the use of the above-discussed operators.

>>> a,b=20,40 
>>> c=a+b 
>>> c 
60
>>> a,b=2.0,4.5 
>>>c=a+b
>>> C
6.5
>>> c+=a 
>>> c
8.5
>>> c-=a 
>>> c
6.5
>>> c*=a 
>>> c
13.0
>>> c/=a 
>>> c 
6.5
>>> c%=a 
>>> c 
0.5
>>> c**=a 
>>> c 
0.25
>>> c//=a 
>>> c 
0.0

Bitwise operators

A bitwise operator operates on one or more bit patterns or binary numerals at the level of their individual bits. Tables 2-5 enlist the bitwise operators with descriptions.

Operator

Description

&

Binary AND operator- Copies corresponding binary 1 to the result, if it exists in both operands.

|

Binary OR operator- Copies corresponding binary 1 to the result, if it exists in either operand.

∧

Binary XOR operator- Copies corresponding binary 1 to the result, if it is set in one operand, but not both.

~

Binary ones complement operator- It is unary and has the effect of flipping bits.

<<

Binary left shift operator- The left side operand bits are moved to the left side by the number on the right-side operand.

>>

Binary right shift operator- The left side operand bits are moved to the right side by the number on the right-side operand.

The following example illustrates the use of the above-discussed operators.

>>> a,b=60,13 
>>> a&b 
12
>>> a | b
61 
>>> a∧b
49 
>>> ~a
-61 
>>> a< < 2
240 
>>> a>>2
15

In the above example, the binary representation of variables a and b are 00111100 and 00001101, respectively. The above binary operations example is tabulated in Tables 2-6.

Bitwise operation

Binary representation Decimal representation

a&b

00001100

12

a | b

00111101

61

a∧b

00110001

49

~a

11000011 -61
a<<2 11110000

240

a>>2 00001111

15

Logical operators

Logical operators compare boolean expressions and return a boolean result. Tables 2-6 enlist the logical operators with descriptions.

Operator

Description

and

Logical AND operator- If both the operands are true (or non-zero), then the condition becomes true.

or

Logical OR’operator- If any of the two operands is true (or non-zero), then the condition becomes true.

not

Logical NOT operator- The result is reverse of the logical state of its operand. If the operand is true (or non-zero), then the condition becomes false.

The following example illustrates the use of the above-discussed operators.

>>> 5>2 and 4<8 
True
>>> 5>2 or 4>8 
True
>>> not (5>2)
False

Membership operators

A membership operator is an operator which tests for membership in a sequence, such as string, list, tuple, etc. Table 2-7 enlists the membership operators.

Operator

Description

In

Evaluate to true, if it finds a variable in the specified sequence; otherwise false.

not in

Evaluate to true, if it does not find a variable in the specified sequence; otherwise false.
>>> 5 in [0, 5, 10, 15]
True 
>>> 6 in [0, 5, 10, 15]
False
>>> 5 not in [0, 5, 10, 15]
False 
>>> 6 not in [0, 5, 10, 15]
True

Identity operators

Identity operators compare the memory locations of two objects. Table 2-8 provides a list of identity operators including a small explanation.

Operator

Description

is

Evaluates to true, if the operands on either side of the operator point to the same object, and false otherwise.

is not

Evaluates to false, if the operands on either side of the operator point to the same object, and true otherwise.

The following example illustrates the use of the above-discussed operators.

>>> a=b=3.1
>>> a is b 
True 
>>> id (a)
3 0 9 8 4 5 2 8 
>>> id (b)
30984528 
>>> c,d=3.1,3.1 
>>> c is d 
False 
>>> id (c)
35058472 
>>> id (d)
30984592
>>> c is not d
True 
>>> a is not b
False

Operator precedence

Operator precedence determines how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. In the expression x=7+3*2, x is assigned 13, not 20, because operator * has higher precedence than +, so it first multiplies 3*2 and then adds into 7.

Table 2-10 summarizes the operator’s precedence in Python, from lowest precedence to highest precedence (from top to bottom). Operators in the same box have the same precedence.

Operator
not, or, and
in, not in
is, is not
=, %, =/, =//, -=, +=, *=, **=
<>, ==, !=
<=, <, >, >=
∧, |
&
>>,<<
+, –
*, /, %, //
∼,+,-
**

Building an RSS feed Scraper with Python

What is RSS?

RSS stands for Really Simple Syndication or Rich Site Summary. It is a type of web feed that allows users and applications to receive regular updates from a website or blog of their choice. Various website use their RSS feed to publish the frequently updated information like blog entries, news headlines etc, So this is where RSS feeds are mainly used.

So we can use that RSS feed to extract some important information from a particular website. In this article I will be showing how you will extract RSS feeds of any website.

Installing packages

You can install all packages using pip like the example below.

 pip install requests
 pip install bs4

Importing  libraries:

Now our project setup is ready, we can start writing the code.

Within our rssScrapy.py we’ll import the packages we’ve installed using pip.

import requests

from bs4 import BeautifulSoup

The above package will allow us to use the functions given to us by the Requests and BeautifulSoup libraries.

I am going to use the RSS feeds of a news website called Times of India.

Link-"https://timesofindia.indiatimes.com/rssfeeds/1221656.cms"

This is basically an XML file.

Building-an-RSS-feed-scraper-with-Python_xml-file

So now I am going to show you how this particular xml file will scrape.

import requests
from bs4 import BeautifulSoup
url="https://timesofindia.indiatimes.com/rssfeeds/1221656.cms"
resp=requests.get(url)
soup=BeautifulSoup(resp.content,features="xml")
print(soup.prettify())

I have imported all necessary libraries.I have also defined url which give me link for news website RSS feed after that for get request I made resp object where I have pass that url.

Now we have response object and we have also a beautiful soup object with me.Bydefault beautiful soup parse html file but we want xml file so we used features=”xml”.So now let me just show you the xml file we have parsed.

Building-an-RSS-feed-scraper-with-Python_output

We dont nedd all the data having in it.We want news description,title,publish date right.So for this we are going to create a list which contains all the content inside item tags.For this we have used   items=soup.findAll('item') 

You can also check the length of items using this len(items)

So now I am writing whole code for scrapping the news RSS feed-

import requests
from bs4 import BeautifulSoup
url="https://timesofindia.indiatimes.com/rssfeeds/1221656.cms"
resp=requests.get(url)
soup=BeautifulSoup(resp.content,features="xml")
items=soup.findAll('item')
item=items[0]
news_items=[]
for item in items:
    news_item={}
    news_item['title']=item.title.text
    news_item['description']=item.description.text
    news_item['link']=item.link.text
    news_item['guid']=item.guid.text
    news_item['pubDate']=item.pubDate.text
    news_items.append(news_item)
print(news_items[2])

So we can see that I have used item.title.textfor scrapping title because item is parent class and title is child class similarly we do for rest.

Each of the articles available on the RSS feed  containing all information within item tags <item>...</item>.
and follows the below structure-

<item>
    <title>...</title>
    <link>...</link>
    <pubDate>...</pubDate>
    <comments>...</comments>
    <description>...</description>
</item>

We’ll be taking advantage of the consistent item tags to parse our information.

I have also make an empty list news_items which append all in it.

So this is how we can parse particularly news item.

Building-an-RSS-feed-scraper-with-Python_final-output

Conclusion:

We have successfully created an RSS feed scraping tool using Python, Requests, and BeautifulSoup. This allows us to parse XML information into a suitable format for us to work with in the future.

Basics of Python – Variable, Identifier and Literal

In this Page, We are Providing Basics of Python – Variable, Identifier and Literal. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Basics of Python – Variable, Identifier and Literal

Variable, identifier, and literal

A variable is a storage location that has an associated symbolic name (called “identifier”), which contains some value (can be literal or other data) that can change. An identifier is a name used to identify a variable, function, class, module, or another object. Literal is a notation for constant values of some built-in type. Literal can be string, plain integer, long integer, floating-point number, imaginary number. For e.g., in the expressions

var1=5 
var2= 'Tom'

var1 and var2 are identifiers, while 5 and ‘ Tom’ are integer and string literals, respectively.

Consider a scenario where a variable is referenced by the identifier a and the variable contains a list. If the same variable is referenced by the identifier b as well, and if an element in the list is changed, the change will be reflected in both identifiers of the same variable.

>>> a = [1, 2, 3]
>>> b =a 
>>> b 
[1, 2, 3]
>> a [ 1 ] =10
>>> a
[1, 10, 3]
>>> b
[1, 10, 3]

Now, the above scenario can be modified a bit, where a and b are two different variables.

>>> a= [1,2,3 ]
>>> b=a[:] # Copying data from a to b.
>>> b 
[1, 2, 3]
>>> a [1] =10 
>>> a 
(1, 10, 3]
>>> b
[1, 2, 3]

There are some rules that need to be followed for valid identifier naming:

  • The first character of the identifier must be a letter of the alphabet (uppercase or lowercase) or an underscore (‘_’).
  • The rest of the identifier name can consist of letters (uppercase or lowercase character), underscores (‘_’), or digits (0-9).
  • Identifier names are case-sensitive. For example, myname and myName are not the same.
  • Identifiers can be of unlimited length.

Python Programming – Introduction to Python

In this Page, We are Providing Python Programming – Introduction to Python. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Introduction to Python

Open-source software

Before stepping into the world of programming using open source tools, one should try to understand the definition of open-source software given by “Open Source Initiative” (abbreviated as OSI). OSI is a non-profit corporation with global scope, formed to educate about and advocate the benefits of open source software, and to build bridges among different constituencies in the open-source community.

Open-source software is a defined as software whose source code is made available under a license that allows modification and re-distribution of the software at will. Sometimes a distinction is made between open source software and free software as given by GNU {http://www.gnu.org/). The detailed distribution terms of open-source software given by OSI are given on the website link: http://opensource. org/.

Python(x,y)

“Python(x,y)” is a free scientific and engineering development software for numerical computations, data analysis, and data visualization based on Python programming language and Spyder interactive development environment, the launcher (current version 2.7.6.0) is shown in figure 1-5. The executable file of Python(x,y) can be downloaded and then installed from the website link: http://code.google.eom/p/pythonxy/. The main features of Python(x,y) are:

  • Bundled with scientific-oriented Python libraries and development environment tools.
  • Extensive documentation of various Python packages.
  • Providing an all-in-one setup program, so that the user can install or uninstall all these packages and features by clicking one button only.

Python Handwritten Notes Chapter 1 img 5

EBNF

A “syntactic metalanguage” is a notation for defining the syntax of a language by the use of a number of rules. A syntactic metalanguage is an important tool of computer science. Since the definition of the programming language “Algol 60”, it has been a custom to define the syntax of a programming language formally. Algol 60 was defined with a notation now known as “Backus-Naur Form” (BNF). This notation has proved a suitable basis for subsequent languages but has frequently been extended or slightly altered.

There are many different notations that are confusing and have prevented the advantages of formal unambiguous definitions from being widely appreciated. “Extended BNF” (abbreviated as EBNF, based on Backus-Naur Form) brings some order to the formal definition of the syntax and is useful not just for the definition of programming languages, but for many other formal definitions. Please refer international standard document (ISO/IEC 14977:1996(E)) for detailed information on EBNF (website link: http://standards.iso.org/ittf/PubliclyAvailobleStandards/).