Mais conteúdo relacionado



Python training

  2. INTRODUCTION • Name :- Kunal Chauhan • Roll no :- 18/BEE/025 • College :- Gautam Buddha University • Topic :- Programming with Python • Training Duration :- 6 weeks ( 15th May to 26th June)
  3. CONTENTS • Introduction to Python programming • Basics of Programming in Python • Principles of Object-oriented Programming(OOP) • SQLite Database, • Developing a GUI with PyQT • Application of Python in Various Disciplines modules
  4. 1. INTRODUCTION TO PYTHON PROGRAMMING Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.). It has syntax that allows developers to write programs with fewer lines than some other programming languages • It is used for: • web development (server-side), • software development, • mathematics, • system scripting.
  5. 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.
  6. 2. BASICS OF PYTHON Python is available on free of cost and easily accessible. To check whether Python is installed in your PC we can use these command lines in command prompt We can perform arithmetic operations in Python IDLE just by using following lines :- >>>2+3 >>>3-1 5 2 >>>4*6 >>>10/5 24 2
  7. This is python idle in which we can perform various arithmetic operations and by opening a new file we can make our programs.
  8. Indentation plays a very important role in Python. Many languages doesn’t care of indentation. It refers to the spaces at the beginning of code line. The Python Interpreter: - Python uses interpreter instead of compiler. In this the interpreter reads program line by line, if any error occurs it will show then and there. The Print statement and input statement: - Print(“Hello world”) X=input(‘Enter your name’)
  9. OPERATORS IN PYTHON comparison logical
  10. CONDITIONAL STATEMENTS 1.If statement Example: -
  11. 2.Else statement
  12. 3.Elif statement :- It combines of if and else. this statement used to reduce the indentation level and make the program less complex.
  13. Loop statements :- these statements are used when we have to operate a block of code which we have to operate a fixed number of times. We have to give condition in loop otherwise it will an infinite loop of no use. There are 2 types of loop in python (a) for loop (b) while loop
  14. WHILE LOOP in the example below it will count until the expression doesn’t equals to 0.
  15. For loop: - This loop is basically used for sequences and range function. in the example given below until the variable x is in the range of 1 to the user input value the program will execute
  16. PYTHON COLLECTIONS (ARRAYS) . • There are four collection data types in the Python programming language: (a) List: - A list is a collection which is ordered and changeable. In Python lists are written with square brackets. Example: - thislist = ["apple", "banana", "cherry"] (b) Tuple: - A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets. Example: - thistuple = ("apple", "banana", "cherry") (c) Set: - A set is a collection which is unordered and unindexed. In Python, sets are written with curly brackets. Example: - thisset = {"apple", "banana", "cherry"} (d) Dictionary: - A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. Example: - thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964}
  17. Functions : - A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
  18. • Creating a function: - In Python a function is defined using the def keyword: def my_function(): print("Hello from a function") • Calling a function : - To call a function, use the function name followed by parenthesis: def my_function(): print("Hello from a function") my_function() • Arguments: - Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
  19. Argument or parameter in Function
  20. • There are 2 types of parameters or Arguments (a) Actual parameters (b) Formal parameters Actual Parameters: - Actual arguments are values(or variables)/expressions that are used inside the parentheses of a function call. Formal Parameters : - Formal arguments are identifiers used in the function definition to represent corresponding actual arguments.
  21. Global and Local variables • Local Variables: - Local variables can only be reached within their scope(like func() above). Like in below program- there are two local variables x and y. def sum(x,y): sum = x + y return sum print(sum(5, 10)) The variables x and y will only work/used inside the function sum() and they don’t exist outside of the function. So trying to use local variable outside their scope, might through NameError. So obviously below line will not work. • Global Variables: - A global variable can be used anywhere in the program as its scope is the entire program. Let’s understand global variable with a very simple example –
  22. z = 25 def func(): global z print(z) z=20 func() print(z) In the above example the variable z can accessed throughout the program. Which means it is a global variable.
  23. USING FUNCTIONS FROM BUILT IN MODULE We've seen function and passing values through them but these are small and simple examples of functions, in real life applications contains thousands of lines of code. And it is impossible for us to find function from the entire code. To tackle these situations modules are used. A module is a file containing definition of function , classes, variables, constants and any other python object. It is used to organize the things. To create module file we have to save the file with .py extension. To use the module file we have to tell the program to get the file, the process of telling program to get the file is known as importing >>>import module
  24. Built in modules: -
  25. • OS Module: - The functions in this module are used to do Operating system tasks such as creating directory, searching contents or creating a directory etc. There are 5 most commonly used function in this module.
  26. • Random module: - The functions in this module depends on the random number generator function random(). • The most common used functions in this module is : -
  27. • Math module: - Constants are pi and Euler's number , that can be accessed as math.pi and math.e
  28. Trigonometric functions in math module: - • Degrees() • Radians() • Sin() • Cos() • Tan()
  29. Logarithmic functions
  30. Representation functions • Floor() • Ceil()
  31. CONSTRUCTING OWN MODULE first we have to save the file containing functions as that is our own module
  32. PACKAGES • In package one or more relevant modules can be save. It is basically a folder containing many module files and it must contains a file which is a packaging unit. • The process of making a package is as follows:- 1. Create a folder 2. Create a sub folder with same name. 3. Place module files inside the subfolder. 4. Create file 5. Create in the parent folder. We can install these packages by: - 1. Open command prompt 2. Change the directory to the folder 3. Then we have to the command : - pip install Then the packages are ready to use in any python program in that PC.
  33. 3. PRINCIPLE OF OBJECT ORIENTED PROGRAMMING (OOP) Objected oriented programming as a discipline has gained a universal following among developers. Python, an in-demand programming language also follows an object-oriented programming paradigm. It deals with declaring Python classes and objects which lays the foundation of OOPs concepts. This article on “object oriented programming python” will walk you through declaring python classes, instantiating objects from them along with the four methodologies of OOPs. Object Oriented Programming is a way of computer programming using the idea of “objects” to represents data and methods. It is also, an approach used for creating neat and reusable code instead of a redundant one. the program is divided into self-contained objects or several mini-programs. Every Individual object represents a different part of the application having its own logic and data to communicate within themselves.
  34. Difference between Object oriented programming and Program oriented programming.
  35. CLASS AND OBJECTS Classes • A class is a collection of objects or you can say it is a blueprint of objects defining the common attributes and behavior. Now the question arises, how do you do that? • Well, it logically groups the data in such a way that code reusability becomes easy. I can give you a real-life example- think of an office going ’employee’ as a class and all the attributes related to it like ’emp_name’, ’emp_age’, ’emp_salary’, ’emp_id’ as the objects in Python. • Class is defined under a “Class” Keyword. Example: class class1(): // class 1 is the name of the class Objects Objects are an instance of a class. It is an entity that has state and behavior. In a nutshell, it is an instance of a class that can access the data. Syntax: obj = class1() Here obj is the “object “ of class1.
  36. • Creating an Object and Class in python: Example: class employee(): def __init__(self,name,age,id,salary): //creating a function = name // self is an instance of a class self.age = age self.salary = salary = id emp1 = employee("harshit",22,1000,1234) //creating objects emp2 = employee("arjun",23,2000,2234) print(emp1.__dict__)//Prints dictionary
  37. Essential Features of OOP: - • Data Encapsulation: - It is the feature that binds the data and functions that manipulate the data. This keeps data and methods of a class or object safe from outside interference and misuse. • Inheritance: - It is the feature that lets you create a new class from a existing class. • Overrididng: - With inheritance you can create a child class from the parent class. This feature lets you redefine a parent class method. Like the name you can override a parent class function or a method in the child class. • Polymorphism: - In OOP when each child class provides its own implementation of an abstract function in parent class, its called polymorphism.
  38. 4. SQlite DATABASE SQlite stands for structured query language . It is type of relational database management system (RDBMS). By which we can create, update, delete, read data. Data can be organized by Database. One of the database is Relational Database which contains tables or entity. The tables in relational database contains attributes and records.
  39. Primary and foreign key:-
  40. • SQlite Studio is the software which was used to create database. This software can be downloaded from and can be accessed easily and the CRUD ( Create, retrieve , update , delete ) can be done.
  41. Accessing Database through python • Python has the standard mechanism to access the Database files called DB-API. Python already contains sqlite3 module in it, we just have to import the module in our program. We can verify if the below program creates a database file ,by opening same file in SQLite Studio.
  42. 5. Developing GUI using PyQt • GUI: - It stands for graphical user interface • CUI : - It stands for character user interface
  43. PyQt • Qt is the GUI application development framework it is used in several well known product like Skype VLC etc. • PyQt is the python binding Qt To install PyQt we have run a command in command prompt.
  44. We need a Qt designer to create GUI windows. The Qt designer can be installed by given procedure :- 1. Open command line window. 2. Run the command: pip3 install pyqt5-tools== 3. Go to your Python installation folder (assuming it is C:python36) 4. You will find a folder called Lib. You will find designer.exe file in it's sub-directories. 5. Go to the folder C:Python36Libsite-packagespyqt5-tools 6. You can find designer.exe in this folder. Run that file
  45. Qt designer
  46. • After we are done with designing the GUI window then we save it. It will save in .ui extension , we have to convert this .ui file to a proper python code. For achieving this we have to use command line utility called pyuic5. We have to save the ui file in the same folder where the pyuic.exe exists. • After this we can get myui,py file
  47. • Now by Qt designer we can design many types of GUI windows, we can manage their layouts for using in different types of devices • Event handler windows can also be designed by this, which basically means that the window will only work when the user moves the cursor and clicks the buttons.
  48. 6. APPLICATION OF PYTHON IN VARIOUS DISCIPLINES MODULES Python supports cross-platform operating systems which makes building applications with it all the more convenient. Some of the globally known applications such as YouTube, BitTorrent, DropBox, etc. use Python to achieve their functionality. • 1. Web Development Python can be used to make web-applications at a rapid rate. Why is that? It is because of the frameworks Python uses to create these applications. There is common-backend logic that goes into making these frameworks and a number of libraries that can help integrate protocols such as HTTPS, FTP, SSL etc. and even help in the processing of JSON, XML, E-Mail and so much more. • 2. Game Development Python is also used in the development of interactive games. There are libraries such as PySoy which is a 3D game engine supporting Python 3, PyGame which provides functionality and a library for game development. Games such as Civilization-IV, Disney’s Toontown Online, Vega Strike etc. have been built using Python. • 3. Machine Learning and Artificial Intelligence Machine Learning and Artificial Intelligence are the talks of the town as they yield the most promising careers for the future. We make the computer learn based on past experiences through the data stored or better yet, create algorithms which makes the computer learn by itself.
  49. • 4. Data Science and Data Visualization • Data is money if you know how to extract relevant information which can help you take calculated risks and increase profits. You study the data you have, perform operations and extract the information required. Libraries such as Pandas, NumPy help you in extracting information. • 5. Desktop GUI • We use Python to program desktop applications. It provides the Tkinter library that can be used to develop user interfaces. There are some other useful toolkits such as the wxWidgets, Kivy, PYQT that can be used to create applications on several platforms. • 6. Web Scraping Applications • Python is a savior when it comes to pull a large amount of data from websites which can then be helpful in various real-world processes such as price comparison, job listings, research and development and much more.