Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Easy Python - Prof. LIKITHA, Cheat Sheet of Public Policy

Python is a computer programming language often used to build websites and software, automate tasks, and analyze data. It is an interpreted language that is cross-platform and has a syntax that is designed for readability, with similarities to the english language and influence from mathematics. Python has a variety of data types, including integers, floats, strings, lists, tuples, dictionaries, and sets. It also supports object-oriented programming concepts such as classes, objects, inheritance, polymorphism, and encapsulation. The basics of python programming, including variables, operators, functions, and exception handling. It also discusses file handling and provides examples of python programs for tasks like atm transactions and student attendance tracking.

Typology: Cheat Sheet

2023/2024

Available from 08/09/2024

likitha-3
likitha-3 🇮🇳

1 document

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Easy Python
1
PYTHON
BASCIS FOR BEGINNER
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Easy Python - Prof. LIKITHA and more Cheat Sheet Public Policy in PDF only on Docsity!

PYTHON

BASCIS FOR BEGINNER

WHAT IS PYTHON?

Python is a computer programming language often used to build websites andsoftware, automate tasks, and analyze data. USED IN FIELDS:  Data science  Data analysis  Machine learning  Web development  Software development Why is Python is Interpreted Language?  A python interpreter is a computer program that converts each high-level program statement into machine code.  Python executes line-by-line instructions and it is a cross-platform. Python Syntax compared to other programming languages:  

Python Syntax:

  1. def func():
  2. statement 1
  3. statement 2
  4. …………………
  5. …………………
  6. statement N Python Indentation Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.Python uses indentation to indicate a block of code. DATA TYPES:Integers : Whole numbers without decimals.  Floats : Numbers with decimals.  Strings : Text enclosed in single or double quotes.  Lists : Ordered collections of items.  Tuples : Immutable collections of items.  Dictionaries : Key-value pairs.

Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set Boolean Type: bool Binary Types: bytes, bytearray, memoryview Strings Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". Boolean Values In programming you often need to know if an expression is True or False.It can evaluate any expression in Python, and get one of two answers, True or False.

Python Oops Concepts: o Classes and Objects - Python classes are the blueprints of the Object. An object is a collection of data and methods that act on the data. o Inheritance - An inheritance is a technique where one class inherits the properties of other classes. o Constructor - Python provides a special method init() which is known as a constructor. This method is automatically called when an object is instantiated. o Data Member - A variable that holds data associated with a class and its objects. o Polymorphism - Polymorphism is a concept where an object can take many forms. In Python, polymorphism can be achieved through method overloading and method overriding. o Method Overloading - In Python, method overloading is achieved through default arguments, where a method can be defined with multiple parameters. The default values are used if some parameters are not passed while calling the method. o Method Overriding - Method overriding is a concept where a subclass implements a method already defined in its superclass. o Encapsulation - Encapsulation is wrapping data and methods into a single unit. In Python, encapsulation is achieved through access modifiers, such as public, private, and protected. o Data Abstraction : A technique to hide the complexity of data and show only essential features to the user. It

provides an interface to interact with the data. Data abstraction reduces complexity and makes code more modular, allowing developers to focus on the program's essential features. Class: A class is a Blueprint of object. Object: It is a instance of class Syntax: class classname Program: class bangalore: def intern(self): print(“work”) def visit(self): Print(“mall of asia”) virat=bangalore() kohli=bangalore() virat.intern() kohli.visit()

hp=laptop() hp.price= hp.processor=”i7” hp.display() 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.

Program: class Person: def init(self, fname, lname): self.firstname = fname self.lastname =l name def printname(self): print(self.firstname, self.lastname) class Student(Person): def init(self, fname, lname): person.init(self, fname, lname) x = Student("Mike", "Olsen") x.printname()

Polymorphism: The word "polymorphism" means "many forms", and in programming it refers to methods/functions/operators with the same name that can be executed on many objects or classes. Program: class Car: def init(self, brand, model): self.brand = brand self.model = model def move(self): print("Drive!") class Boat: def init(self, brand, model): self.brand = brand self.model = model def move(self): print("Sail!") class Plane: def init(self, brand, model): self.brand = brand self.model = model def move(self): print("Fly!")

car1 = Car("Ford", "Mustang") #Create a Car class boat1 = Boat("Ibiza", "Touring 20") #Create a Boat class plane1 = Plane("Boeing", "747") #Create a Plane class for x in (car1, boat1, plane1): x.move() List: Lists are used to store multiple items in a single variable.  Allows duplicate values ,any type of data can be stored, we can modify the list items,we can add or remove the elements @insert() @extend() @append() @pop() Tuples: A tuple is a collection which is ordered and unchangeable.  Allows duplicate values, any types of data can be stored, we can not modify the tuples.

Program: thisdict = { "brand": "Ford", "model": "sux", "year": 2020 } print(thisdict["brand"]) Program : a = (name = "John", age = 36, country = "Norway") print(a.values()) Exception Handling: When a program meets an error it stops an execution of rest of the program. They are five mechansims:

  1. Try-except
  2. Try multiple except
  1. Try except else
  2. Raise exception
  3. Try-except finally  Try-except: Syntax: try: print(x) except: print("An exception occurred") PROGRAM:
  4. x = int ( input ("Enter a number: "))
  5. y = int(input(“ENTER A NUMBER”))
  6. try :
  7. add=x+y
  8. div=x/y print (add) print(div)
  9. except ZeroDivisionError:
  10. print ("Error: Division by zero")
  11. except TypeError:
  12. print (" Invalid input")

File operation: 1.) open() 2.)read() 3.)write() 4.)close() PROGRAM: ATM TRANCATION : if checking_pin(): print("welcom to ATM") print("1.Transaction History") print("2.Withdraw") print("3.Deposit") print("4.Transfer") print("5.Balance") print("6.Exit") while True: choice = int(input("enter your Choice:")) if choice == 1: print("") elif choice == 2: withdraw = float(input(" enter the amount to withdraw"))

if withdraw <= balance: balance -= withdraw print(withdraw,"withdraw.New balance",balance) else: print("Insufficient balance") elif choice == 3: deposit = float(input("enter the amount to deposit")) balance += deposit print(deposit,"deposited.New.balance",balance) elif choice == 4: user2 = int(input("enter user_id to transfer")) transfer = int(input("enter amount to transfer")) if transfer <= balance: balance -= transfer print(balance) elif choice ==5: print("Thankyou have a good day") elif choice == 6: print("invlaid choice") break