Python Original Report
Python Original Report
ARTHUA
[Feb, 2024]
STUDENT DECLARATION
This is to declare that this report has been written by me. No part of the report is
copied from other sources.All information included from other sources has been
duly acknowledged. We aver that if any part of the report is found to be copied, we
DATE: 20/Feb/2024
2
ACKNOWLEDGEMENT
It is our proud privilege and duty to acknowledge the kind of help and
guidance
received from several people in preparation of this report. It would not have
been
possible to prepare this report in this form without their valuable help,
cooperation and guidance.
Last but not the least, we wish to thank our parents for financing our studies
in
this college as well as for constantly encouraging us to learn engineering.
Their Intern
personal sacrifice in providing this opportunity to learn engineering is
gratefully
acknowledged.
3
Table of Content
1 Introduction To Python 6
9
2 Using Variables in Python
1
3 Basics Of Programming in Python
7
4 Principle Of Object-Oriented Programming
2
4
1.Introduction to Python
What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and
released in 1991.
It is used for:
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some
other programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way.
5
Good to know
The most recent major version of Python is Python 3, which we shall be using in this
tutorial. However, Python 2, although not being updated with anything other than security
updates, is still quite popular.
In this tutorial Python will be written in a text editor. It is possible to write Python in an
Integrated Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse
which are particularly useful when managing larger collections of Python files.
Example
Print ("Hello, World!")
Drawbacks of Python
1. SlowExecutionSpeed:Pythonisaninterpretedlanguage,whichmeansthatitcanbe
slower than compiled languages like C++ and Java.
2. MemoryIntensive:Python'sdynamicmemoryallocationcanmakeitmore
memory-intensive than other languages.
6
3. LimitedMobile Development: Python is not a widely used language for mobile
app
4. de ve l op me nt.
LessSecure: Python's open-source nature can make it more susceptible to
security vulnerabilities.
Python Comments
Comments can be used to explain Python code.
Creating a Comment
Comments starts with a, #
and Python will ignore them:
Example
#This is a
comment print
("Hello, World!")
7
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
A comment does not have to be text that explains the code, it can also be used to prevent Python
from executing code:
Mul ti l i ne Co mment s
Python does not really have a syntax for multiline comments.
Example
#This is a comment
#written in
#more than just one
line p ri nt (" Hell o,
Worl d! ")
Python Variables
Var i ab l es
8
A variable is created the moment you first assign a value to it.
Example
x=5
y = "John"
p ri nt (x)
print(y)
Variables do not need to be declared with any , and can even change type
particular after they have been set.
Example
x=4 #xisoftypeint
x = "Sally" # x is now of type str
p ri nt (x)
Variables do not need to be declared with any particular , and can even change type
after they have been set.
Example
x=4 #xisoftypeint
x = "Sally" # x is now of type
str p ri nt (x)
9
Var i ab l e Names
A variable can have a short name (like x and y) or a more descriptive name (age, car
name, total volume). Rules for Python variables:
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)
A variable name cannot be any of the Python keywords.
3.Basics of Programming
Python Lists
List
Lists are used to store multiple items in a single variable.
10
Lists are one of 4 built-in data types in Python used to store collections of data, the
other 3 are Tuple, Set, and Dictionary,all with different qualities and usage.
Example
Create a List:
ListMethods
Python has a set of built-in methods that you can use on lists.
Method Description
11
extend() Add the elements of a list (or any inerrable), to the end of the current list
index() Returns the index of the first element with the specified value
Python Tuples
12
Tupl e
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Set, and Dictionary, all with different qualities and usage.
Example
Create a Tuple:
Tupl e Methods
Python has two built-in methods that you can use on tuples.
Method Description
index() the tuple for a specified value and returns the position of where it was found
Python Dictionaries
13
thi sdict = {
"brand": "Ford",
" model" : "
Mustang" , "year":
1964
}
Dictionary
DictionaryMethods
Python has a set of built-in methods that you can use on dictionaries.
Method Description
14
i tems( ) Returns a list containing a tuple for each key value pair
setdefault() Returns the value of the specified key, with the specified value
Equals: a==b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a >b
15
Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
Example
a=33
b = 200
if b >a:
pri nt (" b i s grea ter t han a")
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this
condition".
Example
a=33
b=33
if b >a:
print("b is greater than a")elif a ==
b: print("a and b are equal")
while loops
for loops
16
The while Loop
With the while loop we can execute a set of statements as long as a condition is true.
Example
Print i as long as i is less than 6:
I=1
while i < 6:
print(i)
i+=1
This is less like the for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Example
Print each fruit in a fruit list:
Python Functions
17
A function is a block of code which only runs when it is
function.
Creating a Function
In Python a function is defined using the
def keyword:
Example
def my_function():
4.Principle of Object-
Oriented Programming
(OOPs)
18
In Python, "oops" typically refers to "Object-Oriented
Programming," which is a programming paradigm that
focuses on the use of objects to represent and manipulate
data. It allows for modular, reusable code that can be easily
maintained and extended
Python Classes/Objects
Python is an object-oriented programming language.
creating objects.
Create a Class
To create a class, use the keyword
class:
Example
Create a class named MyClass, with a property
19
The examples above are classes and objects in their simplest form, and are not really useful
in real l ife appli cat ions.
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
i nit ia ted .
Use the __init__() function to assign values to object properties, or other operations that are
necessary to do when the object is being created:
Example
Create a class named Person, use the __init__() function to assign values for name and age:
class Person:
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
20
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.
Childclass is the class that inherits from another class, also called derived class.
Create a classnamed Person,with firstname and lastname properties, anda printname method:
class Person:
def __init__(self,fname,lname):
self.firstname = fname
self.l astna me = l na me
def printname(self):
print(self.firstname,self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe")
x.printname()
21
5.Final Project
Project: Guess the Number
import random
def guess_the_number():
if guess == secret_number:
print("Congratulations! You guessed the number.")
guessed = True
elif guess < secret_number:
print("Your guess is too low. Try again.")
else:
print("Your guess is too high. Try again.")
guess_the_number()
22
Conclusion
23
Reference
24
25