Monday, 24 June 2019

Special Operators

Special Operators:

Python defines the following 2 special operators:

1. Identity operators
2. Membership operators


#Example program on Identity operators
a=10
b=10
print(a is b) #True
x=True
y=True
print(x is y) #True

Ex: 2:
>>> x=10
>>> y=20
>>> print(x is y)
False
>>> 
We can use identity operators for comparison. We have two identity operators:
is
is not
is operator:
a is b returns True if both a and b are pointing to the same object

Here addresses are comparing.

is not operators:


a=10
b=20

print(a is not b)
True
Ex2:
a=10
b=10
print(a is not b)
False

a is not b returns True if both a and b are not pointing to the same object.

Related Video: https://youtu.be/kyh9qQbf_uk

Next Topic: Membership Operators
Previous topic: Assignment operators


Assignment Operators


Assignment Operators:

a=10 # here = is assignment operator, value 10 is stored in left-hand side variable ‘a’. Now the value of variable ‘a’ is 10.

Definition: Assignment operators are used to assigning the value to the left-hand side variable.

Example:
Ex(1)10=a #Error because variable always should be left-hand side.
Ex(2)
a=10
b=20
a=b # It is valid here b means value i.e 20 of the variable b

Difference between = and ==?
a=10
b=20

=
==
a=b
a==b
i.e we are assigning the value of variable ‘b’ to left hand side variable ‘a’
We comparing two variables, which returns Boolean True/False
Print(a)
print(a==b) or print(b==a) # compares, we get result True or false
Output: 20
Output: True

a+=10 or a=a+10

Shortcut Assignment Operators:

Ex:
a=20
>>> a+=10 #a=a+10àa=20+10àa=30
>>> print(a)
30
>>> 

We can combine assignment operators with some other operator to form a compound assignment operators or shortcut assignment operators

+=
-=
*=
/=
%=
//=
**=
&=
|=
The above is the list of all possible compound operators.

Ordered

How we specified in the object and the same order the data is stored in the memory, same order we will get the output, indexing also applicable.

a=[10,"raj", 12.45,True,2+3j]
print(a)
print(a[0])

#output: Same order only the output displays
10,"raj", 12.45,True,2+3j
10

Immutable

Immutable:

Once we create an object, we can not change in that object. If we try to change then with those changes a new object is created.
This non-changeable is called immutable.


When a object is created, Simple put, a mutable object can be data is changeable after it is created, and an immutable object can't. Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable. Custom classes are generally mutable.


Every object reference holds an object data(instance). When an object is created, it is assigned a unique object address(id). Its data type is defined at runtime and once set can never change, However its state can be changed if it is mutable. Simple put, a mutable object can be changed after it is created, and an immutable object can’t.


Next topic:   List
Previous topic: type casting

Conditional Statements

1.     Control statements

Control statements:
The Control Statements are used for controlling the Execution of the program there are the three control statements 

Divided into 3 parts:
1.      Conditional
2.    Looping or repetitive statements
3.    Unconditional statements

Conditional Statements:

Statements in a programs are executed sequentially. This happens when there is no condition around the statements.
If you put some condition for a statement(s) the flow of execution may change based on the result evaluated by the condition.

They are:
(1.)if (2.) if..else (3.)if…elif..else statements

The if statement:

This statement is used to execute one or more statement depending on whether a condition is True or False

a=1
if a==1:
     print(“one”)
first the condition is checked, if the condition gets True then the if statement is executed.

We can also write group of statements after colon.  The group of statements in Python is called a suite.
While writing a group of statements, we should write them all with proper indentation.


What is Python suite?
A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite.
Indentation means the spaces left before the statements. The default indentation used in Python is 4 spaces.

Header lines begin the statement (with the keyword) and terminate with a colon (: ) and are followed by one or more lines which make up the suite.


For example:
"""wap to accept empno,empname,empsal: Calculate the tax depending upon the
following condition: if salary is greater than 3000/-,
tax should be applied 10% of basic salary
"""
empno=int(input("enter emp no "))
empname=input("enter emp name ")
sal=float(input("enter emp basic salary "))
city=input("enter emp city ")
if sal>=3000:
    tax=0.10*sal
    print("emp no is: ",empno)
    print("emp Name : ",empname)
    print("emp salary :",sal)
    print("emp tax is :",tax)
""" output: case 1:
enter emp no  :007
enter emp name : Sitha
enter emp basic salary: 30000
enter emp city: hyd
emp no is:  7
emp Name :  Sitha
emp salary : 30000.0
emp tax is : 3000.0

Case 2:

enter eno   11
enter ename  krishna
enter salary  2500
enter emp city kadapa
>>> 
>>> 

"""
Observe that every print() function mentioned after colon is starting after 4 spaces only. When we write the statements with same indentation, then those statements are considered as a suite (or belonging to same group).

Indentation:
Indentation is very important in Python. Indentation refers to spaces that are used in the beginning of a statement.

if x==1:

….print(‘a’)

Related Videohttps://www.youtube.com/watch?v=_cS3R_6SbRc&feature=youtu.be

Next Topic:if else statement
Previous TopicControl statements (Related Video: https://www.youtube.com/watch?v=c2AzRkJRruY&feature=youtu.be )

Wednesday, 19 June 2019

Database connectivity SQLite

Database Connectivity:

 #Step1 import the sqlite

import sqlite3 as s

#step2:

conn=s.connect('escdb.db')

print("Database opened successfully")

 Steps:

To create

 Persistanece: OOP’s

Class,object, abstraction,encapsulation,inheritance,polymorphsm…persistence

Files…

Java, python,…

 


To store the data we can use:

Files or database.

 File: A file can data in the secondary memory ex: hard disk. It can store text format or binary format. Data is stored unformatted

 

Database: collection of data. We can store the data in the database

Once the data is stored in a database, we need to perform operations on data.

 

Operations:

Modification to the existing data

Deletion for unnecessary data

Reading /Retrieving the data from the database

These operations is called Database Management systems (DMBS)

 

Dbms=db+application(Java/Python/…)

 

Advantages of DBMS:

 

1.     Storing and retrieving the data is so simple to compared to files by

Using SQL(Structured Query Language) Commands we can perform.

2.     We can not store the huge amount of data in the files whereas we can store in Database

3.     Relationship is not possible to maintain in the files

4.     Multiple users can retrieve or access the database simultaneously.

5.     If any changes are done, we can not rollback to the files

6.     Files are very weak security. Whereas DBMS Offers good security.

 

Example 2:

#creating table

#import the sqlite

import sqlite3 as s

conn=s.connect('escdb.db')

print("Database opened successfully")

conn.execute('''CREATE TABLE APSSDC(

ID INT PRIMARY KEY NOT NULL,

NAME TEXT NOT NULL,

AGE INT NOT NULL,

ADDRESS CHAR(50),

SALARY REAL);''')

print("Table created successfully")

conn.close()

"""

Database opened successfully

Table created successfully

"""

 

Steps to connect to SQLite:

1.     Import the module. Ex: import sqlite3.

2.     Use connect() method of a sqlite3 module and pass the actual parameter  with database name as an parameter/argument. Ex: conn=s.connect('escdb.db')

3.     Perform sql operations: Example: conn.execute(‘’’……’’’’)

 

Import sqlite3: we can use all the classes,method  of sqlite3 module

Sqlite3.connect(): We can create connection to the SQLite Database.

 

If you want to use any database, first we should be installed in our system then only we can create database.

 

We have so many database in the market:

1.     Oracle

2.     GadFly

3.     mSQL

4.     MySQL

5.     PostgreSQL

6.     Microsoft SQL Server 2000

7.     IBM DB2

SAP DB… etc


Tuesday, 4 June 2019

Arithmetic Operators

Arithmetic operators:

Arithmetic operators perform basic arithmetic operations, for example, Addition, subtraction, multiplication, division, % modulus, exponent, etc.

Example:

For arithmetic operators, we will take a simple example of addition where we will add two-digit 4+5=9

Operator
Meaning
Example
Output
+
Addition
a=2+3
print(a): 5
-
Subtraction
a=4-2
print(a): 2
*
Multiplication
a=2*3
6
/
Division: divides left operand by the right operand
a=4/2
2
%
Modulus gives a remainder of division
4%2
0
//
Integer division. Floor division. Performs division and gives only integer quotient.
5//2
2
**
Exponent operator
10**2
100

Priorities of Arithmetic operators:

First priority: *,/,% and floor division are equal priorities.
2nd priority: +,-

Example:
a=2+3*2
-->Here the first '*' operator is evaluated i.e: 3*2 is evaluated i.e: a=2+6
-->Now '+' operator is evaluated i.e a=8
--->Now '=' assignment operator is evaluated i.e 8 is stored in variable 'a'


Youtube channel Related video
Next topic: Example programs on Arithmetic Operators
Previous topic: Operators

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...