0%
Loading ...

Python Tutorials

This Category contains all the tutorials related Tutorials

String in Python

In this tutorial, we would learn about String in Python and Different String methods in Python. A string is a collection of characters. A String can contain alphabets, numbers or special characters. For example, the name of a person or address of a location can be store in a String. How to create a String in Python: To Create a String in Python we can write a sequence of character in Single quote, in double-quotes or even in triple quotes. Triple quotes are used to represent the multi-line String. # Using ” quotes first_string = ‘Owlbuddy’ print(first_string) # Using “” quotes first_string = “Owlbuddy” print(first_string) # Using triple quotes first_string = ”’owlbuddy”’ print(first_string) # using triple quotes first_string = “””Welcome to Owlbuddy.com””” print(first_string) Accessing Characters of String in Python: It is so easy to access Characters of string in Python. we can access characters of a string with the help of index number. For example, we have string “owlbuddy” if we want to access character “w” we know this character on index 1. So like this we can access string characters using an index number. we can also get small parts of a string using slicing operator (:). Check this example. #Creating a string first_string = ‘Owlbuddy’ print(‘first_string= ‘, first_string) #first character print(‘first_string[0] = ‘, first_string[0]) #last character print(‘first_string[-1] = ‘, first_string[-1]) #slicing from 3rd to 5th character print(‘first_string[2:5] = ‘, first_string[2:5]) #slicing from 2nd to 2nd last character print(‘first_string[1:-2] = ‘, first_string[1:-2]) Concatenation of Strings in Python: We can concatenate two different strings using + Operator and we can repeat a string multiple time by using * operator. Check this example. first_string = ‘Hello’ second_string =’World!’ # using + print(first_string + second_string) # using * print(first_string * 3) Changing and deleting to a String: Strings in Python are immutable. We can not change the string after once its initialized. We cannot change String but can delete entire string at once using the del keyword. Check this example. first_string = ‘Owlbuddy’ # TypeError: ‘first_string’ object does not support item assignment first_string[0] = ‘o’ # deleting string using del keyword del first_string # NameError: name ‘first_string’ is not defined print(first_string) Some Common String Methods: Here we will learn some common String methods in Python. Here is a list of methods. lower() upper() join() split() find() replace() first_string = ‘Owlbuddy’ # output: owlbuddy print(first_string.lower()) # output: OWLBUDDY print(first_string.upper()) # output: ‘Hello How are you’ ”.join([‘Hello’, ‘How’, ‘are’, ‘you’]) # output: [‘Hello’, ‘How’, ‘are’, ‘you’] “Hello How are you”.split(); # output: 5 print(first_string.find(‘d’)) # output: Hello Owlbuddy (return copy of string after replacing) ‘Hello World’.replace(‘World’,’Owlbuddy’) Escape Sequence: Escape Sequence in Python starts with a back slash and Python interpreter threat these Escape Sequences different the rest of the string. For example, you want to add a single quote in your string for words like (can’t or don’t) you can not do this by simply adding a single quote you have to add it like (\’) this in the string. It might be confusing right now. Check this example understand Escape Sequence in Python. # using triple quotes print(”’Hello, “What’s up?””’) # escaping single quotes print(‘Hello, “What\’s up?”‘) # escaping double quotes print(“Hello, \”What’s up?\””) List of Escape Sequence in Python: Escape Sequence Description \newline Backslash and newline ignored \\ To add Backslash \’ To add Single Quote \” To add double Quote \” To add double Quote Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

String in Python Read More »

Tuple in Python

In this tutorial, we will learn Tuple in Python. In the last tutorial, we learned about List. Tuples are same as Lists except for one difference that we cannot change elements of tuple after assigning them. In the case of the list we know we can change List elements very easily. It means you can say Tuple is a sequence of immutable Python objects. Like List in Tuple, we can add any number of elements and various kind of elements like(String, Integer, Float, List). Creating a Tuple in Python: In the list, we use square brackets to create List. But in the case of Tuple, we use parentheses. We can also create a Tuple without parentheses. Here we will learn about both ways to Create a Tuple in Python. Check this example program below. # Creating a tuple first_tuple = () # Output: () print(first_tuple) # Initializing Tuple first_tuple = (9, 8, 7) # Output: (9, 8, 7) print(first_tuple) # Tuple with mixed datatypes first_tuple = (9, “Owlbuddy”, 5.5) # Output: (9, “owlbuddy”, 5.5) print(first_tuple) Creating Tuple without parentheses: # Creating a tuple without parentheses first_tuple = 5, “owlbuddy”,10.5 # Output: () print(first_tuple) In case you tuple have only one element then you have to put a comma at the end of the element to use it as a tuple check this example below. first_tuple = (“owlbuddy”) print(type(first_tuple)) # Creating a tuple first_tuple = (“owlbuddy”,) print(type(first_tuple)) # Creating tuple without parentheses first_tuple = “owlbuddy”, print(type(first_tuple)) Accessing elements from Tuple: We can access elements of Tuple using an index number. Like List index number of Tuple elements start from 0. Here is an example to show how we can access elements of Tuple. # Creating a tuple first_tuple = (‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’) # output: O print(first_tuple[0]) # output: u print(first_tuple[4]) Negative Indexing: We can also use Negative indexing access elements of Tuple. Here is a picture which is revealing how negative indexing works in Tuple. The last element of Tuple can we access using -1 Negative Index and second last will have its negative index as -2 and so on for all the elements of Tuple in Python. # Creating a tuple first_tuple = (‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’) # output: y print(first_tuple[-1]) # output: u print(first_tuple[-4]) Slicing of Tuple: We can access a specific range of elements from Tuple using the colon(:) operator. Here is an example of understanding slicing. # Creating a tuple first_tuple = (‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’) # output: (‘l’,’b’,’b’) print(first_tuple[2:5]) # elements beginning to 2nd # Output: (‘O’,’w’) print(first_tuple[:-6]) # elements 5th to end # Output: (‘u’,’d’,’d’,’y’) print(first_tuple[4:]) # elements beginning to end # Output: (‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’) print(first_tuple[:]) Changing a Tuple: We know Tuples are immutable. Mean we can not change elements after once they defined. But if elements of a Tuple are of mutable type like the list. It means we can change nested elements of Tuple. Here is an example of this. first_tuple = (10, 5, 6, [8, 2]) # TypeError: ‘tuple’ object does not support item assignment first_tuple[2] = 10 # But if we change item of mutable element it will not show error first_tuple[3][0] = 12 # Output: (10, 5, 6, [12, 2]) print(first_tuple) Deleting a Tuple: We know Tuples are immutable it means we can not change an element of tuple once they assigned. we also can not delete elements from the tuple. But there is a del keyword available in Python. We can use this del keyword to delete entire tuple. Check out this example. first_tuple = (‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’) # we can not delete elements of Tuple it will show an error del first_tuple[3] # we can an delete an entire Tuple del first_tuple # NameError: name ‘first_tuple’ is not defined print(first_tuple) Use of + and * Operator in Tuple: Like Lists, these two operators are also present in Tuple. we can use + operator to concatenate two Tuples and we can use * operator to repeat a Tuple a given number of times. Check this example first_tuple=(5,6,7) second_tuple=(8,9,10) # Output: (5,6,7,8,9,10) print(first_tuple+second_tuple) # Output: (‘owlbuddy’, ‘owlbuddy’, ‘owlbuddy’) print((“owlbuddy”,) * 3) Some other Tuple Methods: Here are some methods which we can use while working with Tuples. Method Description count(x) Returns how many times a given element present in Tuple index(x) Returns the first index of the given element in Tuple. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Tuple in Python Read More »

List in Python

In this tutorial, we will learn about List in Python. The list is similar to arrays except one difference List can contain a heterogeneous type of data(Integer, Strings and Object) in a single list. In list, we add list element according to the index number. The first element of the list will always save on 0 index. Indexing of list is the same as indexing of array. It means each element of the list will have its own index number. Initialization of List in Python: List in Python can be initialize using square brackets []. We can place our list elements in these square brackets. List in Python doesn’t need a built-in function for the creation of the list. Check this example to understand it. # declaring list first_list = [] # simple list first_list = [10, 15, 20] # list with mixed datatypes first_list = [10, “owlbuddy”,5.5] Nested List in Python: When adding a List as an element in another list it is known as Nested List. Here we will see an example of Nested List. # nested list nested_list = [2,[1,2,3],5] Access elements from a list: Now it’s time to learn how we can access elements of List. There are two ways to access List elements like using List Index, using Negative Indexing etc. we will learn about each way in detail. List Index: We can access elements of a list using List index. List index would always Integer. List index starts from 0 and each element has its own index. For example, if a List has 10 elements it means the index of List will from 0 to 9. #accessing elements of list first_list = [‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’] # Output: O print(first_list[0]) # Output: b print(first_list[3]) Negative indexing: In Python, we can access elements of List using negative indexing. It means the last index of List would have index -1 and second the last index of List would have index -2. Check this Illustration to understand it more clearly. #accessing elements of list first_list = [‘O’,’w’,’l’,’b’,’u’,’d’,’d’,’y’] # Output: y print(first_list[-1]) # Output: u print(first_list[-4]) Adding Elements into Python List: There are various ways to add elements in List like append() method, insert() method, extend() method. We will learn about each method. Let’s start with append. Adding elements in List using append() method: We normally use the append() method to add elements in List. With the help of the append() method, we can add a single element at a time in List. We can also add Tuples to List using this append() method. Check this example of the append() method. #accessing elements of list first_list = [] # adding elements in List using append() method. first_list.append(‘O’) first_list.append(‘w’) first_list.append(‘l’) first_list.append(‘b’) first_list.append(‘u’) first_list.append(‘d’) first_list.append(‘d’) first_list.append(‘y’) # List after adding elements print(first_list) Adding elements in List using insert() method: We use append() method to add an element in List at the last position of List. But if you want to add Element in List at the specific index you can use the insert() method. Check this example of the insert() method. #accessing elements of list first_list = [1,5,8,4,2] # adding elements in List using insert() method. first_list.insert(0,5) first_list.insert(2,6) # List after adding elements print(first_list) Adding elements in List using extend() method: The extend() method is used to add multiple elements at a single time in List. Check this example. #accessing elements of list first_list = [1,5,8,4,2] # adding elements in List using extend() method. first_list.extend([15,14,18]) # List after adding elements print(first_list) Furthermore, we can also use + operator to add concatenate two Lists and we can use * operator to repeat List for multiple times. Check this example. # adding two List using + operator. first_list = [1,5,8] second_list=[5,3,7] # List after adding elements print(first_list+second_list) # Use of * Operator to show List multiple times print(first_list * 2 ) Removing Elements from the List: We can remove elements from List using various methods like remove() method, pop() method and del keyword. We can also use clear() method to clear while list at once. We will learn about each method in detail. Deleting elements from List using remove() method: We can use remove() method to delete an element from the string. Check this example. first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2] # using remove method first_list.remove(5) # list after remove method print(first_list) # removing range of elements from List using remove method for i in range(1, 3): first_list.remove(i) # list after remove method print(first_list) Deleting elements from List using pop() method: first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2] # using pop method first_list.pop(5) first_list.pop(3) # list after remove method print(first_list) Use of clear method in List: We can use clear() method to remove all elements in List. first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2] # using clear method first_list.clear(); # list after remove method print(first_list) Some methods in Python to use with list object: Method Description append() This method is used to add an element at the end of the list. extend() This method is used to add a list into another list. insert() This method is used to add an element in the list at a specific index. remove() This method is used to remove an element in the list. pop() This method is used to remove an element from the specific index in the list. clear() This method is used to remove all elements from the list. index() This method returns to the first index of giving value. count() This method returns the count of the number of items passed as an argument. sort() This method uses to sort a list in ascending order. reverse() This method uses to reverse order of elements in the list. copy() This method returns a copy of the list. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission

List in Python Read More »

Operators in Python

In this tutorial, we will learn about Operators in Python. Operators are special symbols which normally use to perform some special task in the program. For example, you want to add two number. Here we will use + operator to add two numbers or you want to assign value to a variable. Here we will use = assignment operator to assign value to a variable. There are so many operators available in Python and they are divided into different categories. We learn here all the categories. Arithmetic operators: Arithmetic operators are so simple. Arithmetic operators are used to performing simple mathematical computations like addition, subtraction, multiplication and division. There are total of seven operators in this category and these are as follow. Operator Description Example + Addition: This Operator is used to add Two numbers in Python 10+15 – Subtraction: This Operator is used to subtract the second operand from first. 20-10 * Multiply: This Operator is used to multiply two operands. 10*2 / Division: This Operator is used to divide the first operand by second. 10/2 % Modulus: This Operator returns the remainder after dividing the first operand by the second operand. 10%2 // Floor Division: It will return non-decimal value after division e.g. result of 5//2 = 2 5//2 ** Exponent: This operator is used to the first operand raised to the power of the second operand 10**2 Example of Arithmetic Operators in Python: num1 = 20 num2 = 5 # Output: num1 + num2 = 25 print(‘num1 + num2 =’,num1+num2) # Output: num1 – num2 = 15 print(‘num1 – num2 =’,num1-num2) # Output: num1 * num2 = 100 print(‘num1 * num2 =’,num1*num2) # Output: num1 / num2 = 4.0 print(‘num1 / num2 =’,num1/num2) # Output: num1 // num2 = 4 print(‘num1 // num2 =’,num1//num2) # Output: num1 ** num2 = 3200000 print(‘num1 ** num2 =’,num1**num2) Identity Operators: Identity Operators are used to comparing two objects. It will return true if both objects share the same memory location. Operator Description Example is This operator will return true if both objects are on the same memory location. x is y is not This operator will return true if both objects are not on the same memory location. x is not y Example of Identity operators in Python: str1 = “Owlbuddy” str2 = str1 print(str1 is str2) print(str1 is not str2) Membership Operators: Membership operators in Python are used to check if a sequence is present in Object. Operator Description Example in Return true if the given sequence is present in the given object. x in y not in Returns true if a given sequence is not present in Object. x not in y   Example of Membership operators in Python: names = [“Ravi”, “Gurpreet”] print(“Ravi” in names) print(“Hardeep” not in names) Comparison operators: Comparison operators are used to comparing two values in Python and the result of this comparison will come like true or false. Here are some operators which come in this category. Operator Description Example > Greater than: This Operator will return true if left side operand will greater than operand at the right side. 20>10 < Less Than: This Operator will return true if left side operand will be smaller then operand at the right side. 10>20 >= Greater than equal to: This operator will return to if operand at the left side will greater or equal to operand at the right side. 20>=10 <= Less than equal to: This operator will return to if operand at the left side will small or equal to operand at the right side. 10<=20 == Equal to: This Operator will return true if operands of both sides will be equal. 10==10 != Not Equal: This Operator will return true if operands at both sides will not equal. 10!=20   Example of Comparison operators in Python: num1 = 20 num2 = 25 # Output: num1 > num2 is False print(‘num1 > num2 is’,num1 > num2) # Output: num1 < num2 is True print(‘num1 < num2 is’,num1 < num2) # Output: num1 == num2 is False print(‘num1 == num2 is’,num1 == num2) # Output: num1 != num2 is True print(‘num1 != num2 is’,num1 != num2) # Output: num1 >= num2 is False print(‘num1 >= num2 is’,num1 >= num2) # Output: num1 <= num2 is True print(‘num1 <= num2 is’,num1 <= num2) Logical operators in Python: Logical operators are used to making comparison between two expressions. There are three logical operators in Python. Operator Description Example and Logical and: This Operator returns true if both operands will be true x and y or Logical or: This Operator returns true if any one or both operand will be true x or y not Logical not: This Operator return compliment result of the given operand x or y Example of Logical operators in Python: x = True y = False # Output: Result of x and y is False print(‘Result of x and y is’,x and y) # Output: Result of x or y is True print(‘Result of x or y is’,x or y) # Output:Result of not x is False print(‘Result of not x is’,not x) Assignment operators in Python: Assignment operators are used to assigning value to a variable in Python. There is a rich collection of assignment operators in Python. Operator Description Example = Equal to an operator to assign value to a variable. x = 5 += Plus Equal to an operator to assign value to the variable after adding given value in a previous value of a variable. x += 5 -= Minus Equal to an operator to assign value to the variable after subtracting given value from the previous value of a variable. x -= 5 *= Multiply Equal to an operator to assign value to the variable after Multiplying given value with the previous value of the variable. x *= 5 /= Divide Equal to an operator to assign value to the variable after Dividing given value from the previous value of the variable. x /= 5

Operators in Python Read More »

Variables and Data Types

In this tutorial, we will learn about Variables and Data Types in Python. If you have ever learned any programming language than you would have a clear idea about variables and data types. But for those who never learn any programming language before, Variable like containers that contain values and values can change throughout the program. For instance, if you want to add two numbers then you can store those two values in variables and then you can save the addition of those variables in the third variable. Python is totally Object Oriented programming language. It means you do not have to declare every variable before using it. Because every variable in Python is an Object. In this tutorial, we will learn about five kinds of data types. Data Types in Python: Numbers String List Tuple Dictionary Numeric Variables: – This kind of data types are used to store numeric type data. Python supports these four numerical types: int (signed integers) long (long integers, they can also be used to represented octal and hexadecimal) float (floating-point values) complex (complex numbers) String in Python: – String is a collection of characters. Strings can contain alphabets, numeric and special characters. Click Here to Learn String in Detail List in Python: – List is similar to arrays except for one difference. The list can contain different kind of data(integer, string, object) in it. In the list, we can elements according to index number and we can fetch elements from the list using index numbers. Click Here to Learn List in Detail Tuple in Python: – Tuple is the same as List except one difference Tuples are immutable. Once we assigned an element to Tuple we can not change it. We can fetch elements from Tuple using index numbers. Click Here to Learn Tuple in Detail Dictionary: – Python’s dictionaries are kind of hash table type. They work like associative arrays.  Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Variables and Data Types Read More »

Hello World Program

In this tutorial, we will learn how to write a basic hello world program in Python. You may hear from a lot of Python developers that Python is much easier than other programming languages such as Java C++ or swift and that is true. If you ever wrote Hello World program in any other program language then you would find it is so easy to write code in Python. Example Code: print(“Hello World”) As you can see it is so easy to write Hello World program in python just write and Hello Word in double-quotes in the print function and that’s it. As you we haven’t even added any semicolon at the end of the statement. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Hello World Program Read More »

Introduction to Python

In this tutorial, we will learn an introduction to the Python language. Python is a high-level, interpreted and high-level programming language. It is so easy to learn because you would find a lot of English keywords in it. The first time Python was introduced by Guido Van Rossum in 1989. The current version of Python is 3.7.2(in winter 2019). To start with python you have to install it on your machine. You can download Python from this Link Click Here. Python Versions: Python Version Released Date Python 1.0 January 1994 Python 1.5 December 31, 1997 Python 1.6 September 5, 2000 Python 2.0 October 16, 2000 Python 2.1 April 17, 2001 Python 2.2 December 21, 2001 Python 2.3 July 29, 2003 Python 2.4 November 30, 2004 Python 2.5 September 19, 2006 Python 2.6 October 1, 2008 Python 2.7 July 3, 2010 Python 3.0 December 3, 2008 Python 3.1 June 27, 2009 Python 3.2 February 20, 2011 Python 3.3 September 29, 2012 Python 3.4 March 16, 2014 Python 3.5 September 13, 2015 Python 3.6 December 23, 2016 Python 3.7 June 27, 2018 Python 3.5.6 Aug 2, 2018 Python 3.7.2 Oct 20, 2018 Python 3.5.7 March 18, 2019 Python 3.7.3 March 25, 2019 Python 3.6.9 July 2, 2019 Python 3.7.4 July 8, 2019 Features of Python: Before going in deep. Let’s took a quick glance at the features of Python: – Python is a high-level programming language and it is so easy to learn. Debugging Python programs is so easy because it is an interpreter based programming language. Python is a free and open-source programming language. you can download it free of cost from the official website. Python has a very big standard library which makes it so easy and fast to write code in Python. Python has very a giant collection of libraries and it is also one of the best languages for Data Science and Machine learning. Python also provides GUI support like the Tkinter library. We can create desktop applications with the help of Python. Python is the cross-platform programming language you can run python on different platforms like Windows, Linux, Unix and Mac etc. Python Application: We can use python to make applications for various Software development area such as: Web Application: We can create web applications using python. Python provides us with rich collection Frameworks such as Django, Pyramid, Flask we can use these frameworks to create web applications. Desktop Applications: We can create desktop applications with the help of Python. libraries like Tkinter can be used to create GUI for desktop applications. Business Applications: We can also create a business application with the help of python for example e-commerce system. Audio Video Applications: We can also create players to play audios and videos with the help of Python. The basic requirement to work in Python: We need a few things to work in Python. Python should be installed on your machine IDE to run Python programs In this tutorial series, we will use Jupyter NoteBook IDE to write Python programs. You can use any other IDE it will not cause any problem. Click Here to download Jupyter Notebook Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Introduction to Python Read More »

Scroll to Top
×