Thursday 18 June 2020

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("enter any integer value:"))

    f=open("knl.txt","w")

    c=a/b

    print(c)

    f.write("writing %d in knl file" %c)

    #creating a new file name:knl.txt

   

except ZeroDivisionError:

    print("Please give other than zero for b")

finally:

    f.close()


x

Wednesday 17 June 2020

Syllabus

1.Python Introduction
        How to write and execute Python program
       Comment Lines in Python
       Escape Sequences
       Python Identifier
       Reserved word
2.Using Variables in Python
       Variable https://youtu.be/ghJ8t7oKwoU
       Literal
       Python Constants
       Data types https://youtu.be/ghJ8t7oKwoU
         Built-in Data types
         https://youtu.be/ghJ8t7oKwoU
            Python Numbers
            type casting
            Bool data type https://youtu.be/_LeexaIyV1c
            None data type
            Sequences :
               str data type  https://youtu.be/aSeMyoERwOU
               List  https://youtu.be/uwELcLsIeWs
               Tuple https://youtu.be/wwBiOIeGA7M
               Set data type  https://youtu.be/gOWtl3m-Big
               range data type  
               byte data type 
               bytearray
Python Constants
How to download Pycharm software

3. Operators :
    (1.) Arithmetic Operators https://youtu.be/6whfA-ARyMQ
    (2.) Relational Operator https://youtu.be/N-rWkVn9Fqs
    (3.) Logical Operators https://youtu.be/SQxypOkKWyM
    (4.) Assignment operators https://youtu.be/kyh9qQbf_uk
    (5.) Special Operators https://youtu.be/kyh9qQbf_uk
    (6.) Bitwise Operators https://youtu.be/xBNdZ2ytTiU
    Operator Precedence https://youtu.be/q8jc3s7Nmhk

4. Input and output statements https://youtu.be/523O_gX3a-Y
    eval() function https://youtu.be/-AJntqLTi94
    Mathematical Functions

5. Control statements
    1. Conditional Statements https://youtu.be/_cS3R_6SbRc
        if else statement https://youtu.be/VozU5n3ouHM
        if..elif..else statemnet
    2. Loops in Python  https://youtu.be/hFcv96-p4OE
          for loop
            while loop
    3. Unconditional statements https://youtu.be/OdnfITud_6I

      The else suite
Strings and Characters https://youtu.be/KfN8GsNZo1E
Functions https://youtu.be/Gdx870Q6FjM
  Calling a function https://youtu.be/kG5i25P03Z8
  Return statement https://youtu.be/SIwAaHwKctY
  Passing Parameters https://youtu.be/XhsMOvrcuOg
    Formal and actual parameters
      Introduction to *args and **kwargs in Python https://youtu.be/P5NYP1g7PbQ
  Global variables and Local variables https://youtu.be/UJxexYTKGhM
  Recursive Function https://youtu.be/VWgE2KbKF7I
       Tower Of Hanoi https://youtu.be/Pkhmy6eFM0Y
  Lambda function https://youtu.be/8TVWQwEkcFg
    Using Lambda with filter () function https://youtu.be/WgSk2cGcraQ
  Decorator function https://youtu.be/d-qTxNTiZPM
   Assert keyword https://youtu.be/UORaySj-M2w
   Example program on class and object
   https://youtu.be/GkPc0kCsv2o
   Example program on constructors
Files
 • Regular Expressions
Multi-Threading

6. Principles of Object-oriented Programming (OOP)
Overview of OOP
Declaring Class and Creating Object
Understanding Inheritance
Using Magic Methods

7. Database connectivity SQLite

 • Introduction to SQL
Creating an SQLite database
Accessing SQLite Database through Python

8. Developing a GUI with PyQT
GUI and Event driven programming
Qt Designer
Using Common Widgets
Geometry Management
Designing Menu Systems

Clck here to See related videos

Tuesday 16 June 2020

Files in Python

Files :

 To store data in permanently:

1.    Files

2.    Database

 #example program on without using files:

a=input("enter any string ");

print(a)

 Above program only process the data, but it will not store the data permanently in the hard disk. Because it stores in RAM (Random Access memory).

To store data we have to go for the following options:

1.    Files

2.    Using data base

 

Files:

#creating a file with name rfile.txt

f=open("rfile.txt","w")

a=input("enter any string:")

f.write(a)

f.close()

 

File definition:

File is a collection of related information which stores in our hard disk permanently.

 

Advantages:

1.    When the data is stored in a file, it is stored in the memory permanently.

2.    Updating becomes easy. For example we add or delete new data to the existing file, we can delete unnecessary data from the file.

3.    This file can be sharable to other modules or other programs.

4.    Huge amount of data can be stored in a file.

 

Types of Files:              

1.    Text files

2.    Binary Files

Test file store the data in the form of characters.

Binary Files stores the data in the form of bytes.  i.e a group of 8 bits.

It can store text file, image file, audio and video.

 

We have to follow some steps to handling the files:

1.    Open a file

Example: f=open("rfile.txt","w")

2.    Read /Write files

f.write(a)

3.    Closing the file

Example: f.close()

 

Example: 2:

#Reading a file from already existing

#Reading a file from already existing

#step1

f=open("rfile.txt","r")

#step2:

a=f.read(9)

print(a)

print(type(a))

#Step 3:

f.close()

"""

Hello I'm

<class 'str'>

"""

Example 3: using readlin() function

#Ex3: Reading a file from already existing

#step1

f=open("rfile.txt","r")

#step2:

##a=f.read(9)

a=f.readline()

print(a)

print(type(a))

#Step 3:

f.close()

"""

Hello I'm rajendra

<class 'str'>

"""

 

File mode

Meaning

w

To write the data into file. If already exists the file, previous data is deleted with ‘w’ mode.

r

To read the data from the file. Actually fp (file pointer) points to the beginning of the file. Where fp is a variable identifier. It acts as a just like a pointer.

w+

To write and read the data from the file. Previous data is deleted.

a

Appending mode. Adding the new data to the existing file at the end of the existing file. Here f or fp points at the end of the file. If file does’nt exist, it creates a new file for writing the data

r+

To read and write the data into a file. Previous data is not deleted

a+

To append and read data of a file.

x

Exclusive mode. If already exits one file, it will not create the file.

 #Ex3: By using looping to read the data entire

#step1: open the file rifle.txt in read mode.

f=open("rfile.txt","r")

#step2:execute the loop to read entire txt file

#a=f.readline()

for i in f:

    print(i)

#Step 3:

f.close()

"""

Hello I'm rajendra

 

I'm from Vijayawada

 

 

 

Python...

"""

Closing a file: f.close()

Syntax: variable.close()

A file which opened should closed using close() method. If a file is opened but we forgot to close the file, that file may be corrupted or deleted.

 

Writing the file to the existing file:

##Example program on appending the file

f=open("rfile.txt","a")

 

#appending the data to the file

f.write("we are going to write and give viva for python")

 

#closing the opened file

f.close()

 

once you execute the above code, go to your directory check the file.

 

f=open("pyt.txt","x")

print(f)

if f:

    print("file created successfully ")

 

"""

It leads to error if already exists.

Otherwise creates new file

"""

Next: Files part 2

Previous: 

Exceptions:

https://youtu.be/TeVaBIo-WQo

 

 

 


Tuesday 4 February 2020

Exception Handling 3


:
#If there is no exception
try:
   statement1
   statement2
   statement3
except:
   statement4
finally:
   statement5
statement6
output:
st1,2,3,5 and 6 executed

Case 2:
try:
   statement1
   statement2 #exception raised here
   statement3
except: #corresponding exception block is matched
   statement4
finally:
   statement5
statement6
output:
st1,4 ,,5 and 6 executed and Normal termination
Case 3:
try:
   statement1
   statement2 #exception raised here
   statement3
except: #corresponding exception block is not matched
   statement4
finally:
   statement5
statement6
output:
st1, 5 executed and Abnormal termination

#Case 4:Exception is raised in exception block
try:
   print("statement1 ")
   print("statement2 ")
   print("statement3")
except ZeroDivisionError:
   print(2/0)#exception raised here
finally:
   print("statement 5")
print("statement6")
"""output:
statement1
statement2
statement3
statement 5
statement6
#Case 5:Exception is raised in finally block
try:
   print("statement1 ")
   print("statement2 ")
   print("statement3")
except ZeroDivisionError:
   print("Statement 4")
finally:
   print(2/0)#exception raised here
print("statement6")
"""output:
statement1
statement2
statement3
Abnormal termination
"""
There is only two situation where finally block is not executed i.e when exception is raised in finally block, and os._exit(0) function.
Example:
#Case 5:finally is block is not executed when we use ox._exit(0)
import os
try:
   print("statement1 ")
   print("statement2 ")
   os._exit(0)
   print(2/0)
except ZeroDivisionError:
   print("Statement 4")
finally:
   print("Statement 5")
print("statement6")
"""output:
statement1
statement2
Nested try –except-finally blocks
"""
#Nested try blocks
try:
    print("outer try block")
    try:
        print("inner try block")
        print(2/0) #exception raises here, and corresponding except is matched
    except ZeroDivisionError:
        print("inner except block")
    finally:
        print("inner finally block")
except:
    print("outer except block")
finally:
    print("outer finally block")

"""output:
outer try block
inner try block
inner except block
inner finally block
outer finally block
>>>
"""
We can take try-except-finally blocks inside try or except or finally blocks it is called nesting exception handling.
Generally risky code we have to take inside outer try block and too much risky code we have to take inside inner try block.

·       Inside inner try block if an exception raised then except block is responsible to handle.  If it unable to handle then outer except block is responsible to handle.
Syntax:
try:
      ……
      ……
      try:
       ……..
       ………
       except:
       ……..
       ………
except:
      ……
      ……

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

Next: User Defined Exception
Previous:Exception Handling part 2


Tuesday 28 January 2020

Exception Handling Part 2

#If there is no exception
try:
    print("Statement 1")
    print("Statement 2")
    print("statement 3")
except ZeroDivisionError:
    print("Statement 4")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 2
statement 3
Statement 5
Statement 6

Note: If there is no exception, all the statements will be executed, but not except block
"""
Case 2:#If there is exception at statement 2
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3") #Not executed
except ZeroDivisionError:
    print("Statement 4")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 4
Statement 5
Statement 6
Note: If any exception raised within the try block, rest of the try block statements are not executed.
"""

Case 3:#If there is an exception at st 2, but the corresponding exception is not matched
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3")
except TypeError:
    print("Statement 4")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 5
Abnormal termination
"""
#Case 4:If there is exception at st 2
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print("Zero Division is not possible")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Zero Division is not possible
Statement 5
Statement 6

#Case 4:If there is exception at st 2, and exception is in except block
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print(2/0)#Exception
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 5
Abnormal Termination
"""
Note: Exception is at statement 2 and also in except block, in addition, to try block, there may be a chance of raising exception inside except and finally block also, it leads to abnormal termination.
"""
#Case 5:If there is no exception in try block
try:
    print("Statement 1")
    print(10/2)#statement 2
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print(2/0)#Exception
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
5.0
statement 3
Statement 5
Statement 6
"""
#Case 6:exception is raised out side the try block, which leads to abnormal
#termination
try:
    print("Statement 1")
    print(10/2)#statement 2
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print("Statement 5")
finally:
    print("Statement 6")
print(10/0)#st 7

"""output:
Statement 1
5.0
statement 3
Statement 6
Abnormal termination
"""
#Multiple except blocks
try:
    a=int(input("enter a value"))
    b=int(input("enter b value"))
    print(a/b)
except ZeroDivisionError:
    print("b=",b,"should not be zero")
except ValueError:
    print("Enter integer only")
"""output:
Case1:
enter a value4
enter b value2
2.0
Case 2:
enter a value4
enter b value0
b= 0 should not be zero
Case 3:
enter a value4
enter b valuetwo
Enter integer only
"""
Combination except blocks also can be written
#Multiple except blocks
try:
    a=int(input("enter a value"))
    b=int(input("enter b value"))
    print(a/b)
except (ZeroDivisionError,ValueError):
    print("b=",b,"should not be zero")
    print("Enter integer only")

"""output:
Case1:
enter a value4
enter b value2
2.0
Case 2:
enter a value4
enter b value0
b= 0 should not be zero
Case 3:
enter a value4
enter b valuetwo
Enter integer only
"""
Default exception:
We can use default except block to handle any type of exceptions.
#default except
try:
    a=int(input("enter a value"))
    b=int(input("enter b value"))
    print(a/b)
except ZeroDivisionError:
    print("b=",b,"should not be zero")
except: #default exception
    print("Enter integer only")

"""output:
Case1:
enter a value4
enter b value2
2.0
Case 2:
enter a value4
enter b value0
b= 0 should not be zero
Case 3:
enter a value4
enter b valuetwo
Enter integer only
"""
    Related Video: https://youtu.be/dq9W6ig91QQ

Next: Exception Handling 3
Prev:Exception Handling part1
https://youtu.be/oC6v7WLzZFw

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(&qu...