Skip to main content

Posts

Showing posts from June, 2021

How to delete files in python

Hi everyone. Welcome back to another session on python. If you are new here, please do checkout our previous articles on python and other programming languages. Also subscribe to our YouTube channel for the video tutorials. In this tutorial, we are going to learn how to do the following in python Deleting files in python Checking if files still exist Deleting folders in python Deleting files in python To delete a file in python, we can use the remove() which is called from the os module . This remove() method takes only one arguments. syntax os.remove(file_name) Let’s demonstrate with an example import os # Delete file2.txt os.remove(“file2.txt”) After you must have deleted a file and you want to confirm if the file has been deleted or you might to check if the file still exist before deleting. Here is an example to check if a file has been deleted or still exist import os if os.path.exists("file2.txt"): os.remove("file2.txt") else: print("The file does n...

Python math functions and modules

Hi guys. Welcome to another session of python programming. If you are new here please do checkout our previous on python. Also check out our YouTube channel to get the full tutorial on this article. In this session we are going to see the different math functions and modules in python. Python has a variety of default functions which do not require any import module. Some of this predefined functions are: input(), int(), float(), type() and many others. The syntax for this python math module and functions is given below Python math module Python has an already default module known as math module which has a list of predefined functions to do mathematical calculations. To first work with this math module, we need an import statement:   import math Below is a list of table showing some math’s function pow math.pow(base,exp) This function returns base raised to power exp. Error occurs if (base=0 and exp<=0) or (base<0 and exp is not integer). math.pow(8,2) gives 64 ceil math.cel...

Introduction to Numpy python library

  Hi everyone. Welcome to this very interesting tutorial on python. If you are new on this platform please do checkout out previous tutorials on python and subscribe to our YouTube channel to get the full video tutorials of this tutorial. We have been learning about the basic syntax, functions and packages in python, but we have now to come to an interesting part in python programming which is one of the python package manager known as Numpy python module. In this article we are going to discuss: Getting started with Numpy( what is Numpy ) Installing Numpy Advantages of Numpy How to import Numpy Getting Started with Numpy (what is Numpy) Numpy (numerical python) is the primary Python package for performing scientific computing. It has very powerful tools for integrating C/C++, linear algebra and N-dimensional array. Despite its simple name, NumPy is a powerful Python package whose main aim is working with arrays and matrices. Some of the advantages of numpy Numpy can be used in pyt...

how to create and write files in python

Hi guys welcome back to another interesting session on this platform. Today we are going to see how we can write and create files in python. If you are new in this platform, please do checkout our previous articles and also subscribe to our YouTube channel for the video tutorials. Creating text files in python is quite easy.  We have learned how to open and read a file. Let’s now try writing into a file.  They are two main ways of writing into a file which are  ‘a’ (append) mode ‘w’ mode. When you use this mode, you will erase or delete all the previous content that is exiting in the file. Let’s take an example with both cases f = open (“myfile.txt”, “a”) f.write(“python is really awesome”) f.write(“python is fun”) f.close() When the above program is executed, the compiler will ask you to save the file by specifying the name of the new file you are creating. In our case we saved it as myfile. When you open the folder created, you will see the program that we wrote and...

Accepting user input in python

Hi guys. Welcome to another amazing tutorials on python. If you are new in this platform and haven’t checked out previous tutorials, please do checkout our previous tutorials on python and other programming languages and subscribe to our YouTube channel for the video tutorials. To get more of our content, you can check out our Facebook group and page. We also offer training for developers.  In this article we are going to see how to create sentences and other outputs by just inputting information into a program. We all might have used user’s input to work knowingly or unknowingly. Input can come from a variety of sources such as keyboard, mouse clicks, the internet, database, or external storage. The keyboard is the most commonly used channel. The input from the keyboard is read using the input() built-in function. The input from the user is read as a prompt string and is being stored as a variable Syntax input(prompt) Once the function is called, the prompt string is displayed on...

How to create and use modules in python

Hi. Welcome to another interesting session. If you are new please do checkout previous articles on python and other programming languages. And also subscribe to our YouTube channel for the video tutorials. In this article you will learn: What is a module? How to create and import custom made modules in python Find the different ways to import and use custom built-in modules in Python. Defining a module Let’s start by defining modules in python Modules contain statements and definitions in python that is, any file that contains proper Python code and has the .py extension can be called a Python module.  They usually contain objects such as classes and functions. Modules help break down large programs into manageable files. They also promote code reusability Creating a module Let us create our first module. Modules are generally known by their main filename, that is, without the .py extension. For example, let’s create a module and save it as multiplier.py. def multiply (a, b): prod...

Lambda Functions in Python

Hi guys welcome back to another exciting session on this platform. If you are new, please do check out our previous articles on python and also subscribe to our YouTube channel for the video tutorial. In this session, we are going to discuss the following about lambda functions. What are lambda functions? Lambda syntax How they are use and how they are created. Lambda functions A lambda functions also called an anonymous function, are functions which are defined without a name.  While you would define a function with the normal ‘def keyword’ instead lambda functions are defined with the lambda keyword . They are mostly used when we need a nameless function for a short period of time. Syntax of the lambda function Lambda arguments: expression You can use a lambda function anywhere a function is needed. Here is an example of a program that uses the lambda function that squares the values of the input squared = lambda x: x**2 print(squared(10)) OUTPUT In the above program, lambda x: ...

Python Tuples

Hi everyone welcome back to another amazing lesson on python. If you are new, please do checkout our previous articles to get a better understanding of what we are going do today and also subscribe to our YouTube channel for the video tutorials. In the previous lesson, we saw a python list, how they are accessed, it different inbuilt functions. Unlike a list that we have just seen, we won’t be able to change elements in a tuple once they have been created. In the creation of a tuple, we will use parentheses instead of square brackets. Syntax for a tuple Mytuple=() How to create a tuple As we earlier said, we are going to use parentheses instead of square brackets. We will place items within the parenthesis and separate them with a comma Example Mytuple_x=(10,9,8,7,6,5,4) Note that, we can also create a tuple without the parenthesis and also create empty tuple Example numbers=5,3,4,2,1   Tuple membership test This membership is not different from the one which we have seen in python...

Python sets

Hi everyone, welcome back to another exciting lesson. Hope the previous lessons have been helpful. If you are new here please, do checkout our previous lessons and subscribe to our YouTube channel for the video tutorial. Today, we are going to look at another exciting program in python called python set.  A set is an unordered group of unique elements which are used to carry out math operations involving sets such as intersection, union, or symmetric difference. In this session we are going to see how sets are created, its syntax and its set operation, its various user-built functions. Creating a set You can create a set by putting all elements in curly braces {} or by using set () , one of python’s built-in function. A comma is used to separate items from each other.  Syntax of a set My_set={} An example of a set of integers my_set=(1, 2, 5, 6, 10, 15) An example of a set of strings my_set={‘a’, ‘e’, ‘w’, ‘d’, ‘g’, ‘u’} Changing elements on a set We are able to change ele...