















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 23
This page cannot be seen from the preview
Don't miss anything!
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:
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:
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