Contenu connexe

First Steps in Python Programming

  1. First Steps into Python Programming Facilitator: Agbo Dozie
  2. This May Come to Mind… when you hear Python
  3. But its more like this…
  4. Table of Content 1 2 3 4 5 6 7 8 Python. Installation. Practical Uses Why Python? Variables, Datatypes (Strings, Int, Float, List, Tuples, Bool, and Dictionaries) & Formatting Functions & Recursions in Python Arithmetic Operators, Comparators, Loops, & Conditionals File Handling in Python Modules in Python Project & Setup Github
  5. 1 Python, Installation & Practical Uses
  6. | Python. IDEs. Installing Python What is Python? of significant whitespace, as well as the DRY (Don’t Repeat Yourself) principle. Wikipedia (Modified) Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python. Even some Windows computers (notably those from HP) now come with Python already installed. To check if you have Python on your computer, just open your command prompt and type “Python”. You would see the result below, otherwise you do not have Python installed. In order to download Python however, visit the official Python website and download the latest version. During installation, watch out and ensure to check the box labeled “Add Python to PATH”. Installing an IDE Installing Python IDEs are the environment in which we would write Python codes. Python comes with its default IDE called IDLE, which like the acronym IDE also stands for “Integrated Development Environment”. Below are some of the most common IDEs. From VSCode to Atom Editor to PyCharm, and to the very lightweight Sublime Text. Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use
  7. | Practical Uses of Python Web Development Python can be used to make web- applications at a rapid rate using frameworks, such as Flask and Django. Game Development There are libraries such as PySoy and PyGame which is a 3D game engine supporting Python 3. Machine Learning & AI Machine Learning and Artificial Intelligence are the talks of the town as they yield the most promising careers for the future. Data Science & Data Visualizations Libraries such as Pandas, NumPy help you in extracting information. We can also use libraries like Matplotlib and Seaborn for visualizations. Desktop GUI Python can be used to program desktop applications. It provides the Tkinter library that can be used to develop user interfaces. Webscraping Applications Python can be used to pull a large amount of data from websites which can then be helpful in various real-world processes. Business Applications Business Applications cover domains such as e-commerce, ERP and many more. Audio & Video Apps Python can be used to develop Video and audio applications such as TimPlayer and Cplay. These apps often provide better stability and performance compared to other media players. CAD Applications Computer-Aided Designs apps can be complicated, but Python makes this simple. Example is Fandango. Embedded Apps Python is based on C which means that it can be used to create Embedded C software for embedded applications.
  8. 2 Why Python?
  9. | Why Python? ▪ Python is easy to learn ▪ It has a vast community of users ▪ There is plethora of ‘fancy’ career paths for Python developers ▪ Python can be used for Web development ▪ Python is useful in AI and Machine Learning ▪ Python has millions of modules and frameworks ▪ There has been a lot of adoption and shifting by Startups and Corporates to Python
  10. | Why Python? try: aFile = open('anyOldFile.txt') myString = aFile.readline() print('File contents are: ') print(myString) except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) #include <iostream> #include <exception> using namespace std; int main () { int* myarray = NULL; if (myarray) { cout << "Not NULL" << endl; } else { cout << "Is NULL" << endl; } try { int* myarray = new int[1000000000]; } catch (exception& e) { cout << "Standard exception: " << e.what() << endl; if (myarray) { cout << "Not NULL" << endl; } else { cout << "Is NULL" << endl; } } if(myarray) { cout << "Deleting myarray" << endl; delete [] myarray; } cout << "Returning" << endl; return 0; } Python C++
  11. 3 Variables, Datatypes (Strings, Int, Float, List, Tuples, Bool, and Dictionaries) & Formatting
  12. | Variables Variables are containers for storing data values. Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. ▪ A variable name must start with a letter or the underscore character ▪ A variable name cannot start with a number ▪ 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) RULES for Naming Variables
  13. | Datatypes Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories: Text Type: str Numeric Types: int , float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview
  14. | Formatting String Concatenation String Methods String Formatting There are other string methods from len() to upper(), to lower(). In fact, you can use the dir(string) command to get the methods associated to strings. There are other string formatting techniques as well. There’s the %s method as well as the direct concatenation approach.
  15. 4 Arithmetic Operators, Comparators, Loops, & Conditionals
  16. | Arithmetic Operators Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators Addition Subtraction Multiplication Division Modulus Exponent Floor Division == != <> > < >= <= = += -= *= /= %= **= //= in not in Logical AND Logical OR Logical NOT is is not
  17. | Loops Python has two primitive loop commands: • while loops • for loops A loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). 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. for for for With the while loop we can execute a set of statements as long as a condition is true.while
  18. | Conditionals Python conditionals are used to test if certain conditions or expressions check out. For example, the popular litmus tests of chemistry. In the test, the paper turns red when the paper is dipped in an acid and blue when dipped in a basic solution. Similarly, in conditionals, we use the if and else keywords for “action” and “reaction” respectively.if else You must have noticed the elif keyword; it is not mandatory; you only use it if you have more conditions to test. In other words, your if/else block would work fine without it. elif Read up: Nested Ifs.
  19. 5 Functions & Recursions in Python
  20. | Functions in Python 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. In Python a function is defined using the def keyword: To let a function return a value, use the return keyword.
  21. | Functions in Python Contd. Example: By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less. Give a function all it wants and it would answer your call, otherwise, it spits out errors.
  22. | Recursions ▪ Python also accepts function recursion, which means a defined function can call itself. ▪ Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result. ▪ >> The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion can be a very efficient and mathematically- elegant approach to programming. We consider and example below: What does the recursion below do?
  23. 6 File Handling in Python
  24. | File Handling in Python File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files. We would consider the 4 operations below, i.e, CRUD. The key function for working with files in Python is the open() function. The open() function takes two parameters; filename, and mode. Let’s see the syntax below: Open To open the file, use the built-in open() function. The open() function returns a file object, which has a read() method for reading the content of the file: Read Note: You can return one line by using the readline() method. Also it is always good practice to close a file when you are done using it. To do this, use the close method, i.e f.close().
  25. | File Handling in Python Contd. To write to an existing file, you must add a parameter to the open() function: "a" - Append - will append to the end of the file | "w" - Write - will overwrite any existing content Note that new files would be created if they do not already exist. Write To delete a file, you must import the OS module, and run its os.remove() function:Delete
  26. 7 Modules in Python
  27. | Modules in Python A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code. Modules are all about reusability. Take for example the code below: Save this code in a file named To use the module, we use the import statement. You could wish to import only a function or variable from your module. That wish can be granted without a genie, easily. You see? Just use from and import!
  28. 8 Project & Setup Github
  29. | Project & Setup Github Also called “The New Resume”, GitHub is a git repository hosting service. Simply, Github helps you do version control while coding. Most employer’s now ask to see your GitHub repository when applying for developer roles. Setting Up GitHub ▪ Create a GitHub Account ▪ Download and install git from Choose your laptop’s specification (OS) ▪ Login in GitHub Username and Password in local machine and start We would be more exhaustive on this in our demo session. Cheers! Project: i. An iPython Notebook has been attached and shared. Study the items in it and work the problem underneath. ii. Write a recursion for printing a list of first n Fibonacci numbers. committing changes and pushing files to GitHub!
  30. Thank you for Listening… Any Questions? Contact me on: