0% found this document useful (0 votes)
14 views13 pages

Python Logic Oops File2

The document covers fundamental concepts of Python programming, including control flow, loops, conditionals, and the use of the pass statement. It explains object-oriented programming principles such as classes, inheritance, and iterators, as well as exception handling with try-except blocks. Additionally, it discusses user input methods in Python 3.6 compared to Python 2.7.

Uploaded by

bisu11042
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Python Logic Oops File2

The document covers fundamental concepts of Python programming, including control flow, loops, conditionals, and the use of the pass statement. It explains object-oriented programming principles such as classes, inheritance, and iterators, as well as exception handling with try-except blocks. Additionally, it discusses user input methods in Python 3.6 compared to Python 2.7.

Uploaded by

bisu11042
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PYHTON PROGRAMMING

Python logic
CONTROL FLOW, LOOPS

Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chained

conditional (if-elif-else); Iteration: while, for, break, continue.

1)while loop:

i=1

while(i<10):

print(i)

i=i+1

2) for loop

for i in range(1,10):

print(i)

2
3

3) break

i=1

while(i<10):

print(i)

i=i+1

if(i==4):

break

4)continue

i=1

while(i<10):

print(i)

i=i+1

if(i==4):

continue

1
2

Python pass Statement


Last Updated : 19 Sep, 2023



The Python pass statement is a null statement. But the difference
between pass and comment is that comment is ignored by the
interpreter whereas pass is not ignored.
a = 10

b = 20

if(a<b):

pass

else:

print("b<a")

2) i=1
while(i<10):
if(i==5):
continue
print(i)
i=i+1

1
2
3
4

3)pass
for i in range(1,10):
if(i==5):
pass
else:
print(i)

1
2
3
4
6
7
8
9

Python Classes/Objects
Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class
To create a class, use the keyword class:

The __init__() Function


The examples above are classes and objects in their simplest form, and are not
really useful in real life applications.

To understand the meaning of classes we have to understand the built-in


__init__() function.

All classes have a function called __init__(), which is always executed when the
class is being initiated.

Use the __init__() function to assign values to object properties, or other


operations that are necessary to do when the object is being created:

Object Methods
Objects can also contain methods. Methods in objects are functions that belong
to the object.

Let us create a method in the Person class:

Python Inheritance
Inheritance allows us to define a class that inherits all the methods and
properties from another class.

Parent class is the class being inherited from, also called base class.

Child class is the class that inherits from another class, also called derived
class.
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other
class:
Python Iterators
An iterator is an object that contains a countable number of values.

An iterator is an object that can be iterated upon, meaning that you can
traverse through all the values.

Technically, in Python, an iterator is an object which implements the iterator


protocol, which consist of the methods __iter__() and __next__().

Iterator vs Iterable
Lists, tuples, dictionaries, and sets are all iterable objects. They are
iterable containers which you can get an iterator from.

All these objects have a iter() method which is used to get an iterator:

Example
Return an iterator from a tuple, and print each value:

Even strings are iterable objects, and can return an iterator:

Looping Through an Iterator


We can also use a for loop to iterate through an iterable object:

Python Try Except


❮ PreviousNext ❯
The try block lets you test a block of code for errors.

The except block lets you handle the error.

The else block lets you execute code when there is no error.

The finally block lets you execute code, regardless of the result of the try-
and except blocks.

Exception Handling
When an error occurs, or exception as we call it, Python will normally stop and
generate an error message.

These exceptions can be handled using the try statement:

#The try block will generate an error, because x is not defined:

try:
print(x)
except:
print("An exception occurred")

An exception occurred

ince the try block raises an error, the except block will be executed.

Without the try block, the program will crash and raise an error:

#This will raise an exception, because x is not defined:

print(x)

Traceback (most recent call last):


File "demo_try_except_error.py", line 3, in <module>
print(x)
NameError: name 'x' is not defined

Many Exceptions
You can define as many exception blocks as you want, e.g. if you want to
execute a special block of code for a special kind of error:
Example
Print one message if the try block raises a NameError and another for other
errors:

#The try block will generate a NameError, because x is not defined:

try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")

Variable x is not defined

Else
You can use the else keyword to define a block of code to be executed if no
errors were raised:

Example
In this example, the try block does not generate any error:

#The try block does not raise any errors, so the else block is executed:

try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")

Hello
Nothing went wrong

Finally
The finally block, if specified, will be executed regardless if the try block raises
an error or not.

#The finally block gets executed no matter if the try block raises any errors or not:

try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")

Something went wrong


The 'try except' is finishe

Python User Input


❮ PreviousNext ❯

User Input
Python allows for user input.

That means we are able to ask the user for input.

The method is a bit different in Python 3.6 than Python 2.7.

Python 3.6 uses the input() method.

Python 2.7 uses the raw_input() method.

The following example asks for the username, and when you entered the
username, it gets printed on the screen:

username = input("Enter username:")


print("Username is: " + username)

Enter username:
Username is: dipali

You might also like