{

Learn free python tutorials


How to Convert List to String with comma delimiter python with example

February 28, 2022 ·  2 min read

This post shows you an example to convert a List to a String with a comma delimiter. This also includes concatenating string lists to Single string with examples. Python Convert List to String examples you can convert List to String in multiple ways, One way is using String join with a delimiter such as a comma, or hyphen. Generate Comma separated string from a List of strings in Python The string contains a join method that appends the iterable strings object such as list and array....


How to current Date and time (Local, UTC timezone) python with example

February 28, 2022 ·  4 min read

This post shows you multiple ways to get the Current Date and Time in Python How to get the Current Date and time in the Local, UTC timezone Python First, import the DateTime module into the code base. The DateTime class has a now() method that returns the Date and Time in the Local timezone. import datetime now = datetime.datetime.now() print(now) if you want to get datetime in UTC timezone, you can call the utcnow() method...


How to current Date and time in python with example

February 28, 2022 ·  1 min read

This tutorial explains multiple ways to check List is empty or not Empty List contains zero elements in size and is denoted as [] For example, an Empty list can be declared as follows list=[] In Real applications, Data comes from an API and returns a List. Check List is empty or not There are multiple ways we can check List is empty or not. Here is an example list=[]; if not list the: print("Empty List") Output:...


How to delete a file or folder in python with example

February 28, 2022 ·  3 min read

This tutorial explains file and folder delete in python with code examples. Contents Python File delete example How to delete Empty folder or directory in Python example How do you delete files and subfolders in directory How do you delete a file in Python? File delete is an file operation that delete a file from a directory. directory delete is an operation that delete files and folders in a directory...


How to get character position(index) in a string python with example

February 28, 2022 ·  3 min read

This tutorial explains about character’s position of a given string in Python language. The string is a group of characters enclosed in double quotes. Characters in a string are located with starting index of 0. For example, Starting character position is 0. How to Get character position in a String in python? use find() function String find function checks the given substring position and returns the position if a matching substring is found....


How to Get File size in bytes, KB, MB, GB, TB format, python with example

February 28, 2022 ·  2 min read

This short post explains multiple ways to get the size of a file in Python with examples Python File Size Example To get file size in bytes, Python provides multiple ways. The first way is using os.path.getsize() function Syntax: os.path.getsize(filepath) Arguments: filepath: Path of a file. Returns: Returns file size in bytes. throws OSError if the file is not found or no file read access permission Here is an example...


How to get the First and Last Element of a List in python with example

February 28, 2022 ·  3 min read

A list is a data structure to store the collection of elements in the insertion order. This tutorial explains about following things in Python with examples How to get the First object from a List How to get the Last Element from a List How to get any position element from a List. Elements in a List always start with index=0 to index=len(list)-1. For example, the first element index is zero, Last element index is the length of a list minus 1....


How to get the First and Last Element of a List in python with example

February 28, 2022 ·  2 min read

The list contains elements or a list of sub-lists or a Map i.e two dimensions list. Flatten list is to output a single list from a two-dimension list. Let’s create a list of sub-list or two-dimensional lists in python. list = [[1, 2], [3],[4,5,6],[7],[8,9,10]] A two-dimensional list contains a list of elements, where each element is again a list. After flatting the list, the result is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Let’s see multiple ways to flatten a list in Python...


How to get the index in for loops python with example

February 28, 2022 ·  2 min read

This tutorials explains about multiple ways to get an index in for loop iteration in list for loop. Python for loop index There are multiple ways we can get index in for loop list. use native for loop with counter Declare counter variable that defaults to zero Iterate each element and print the index and iterated element Increment the counter by 1 inside for loop Here is an example counter = 0 for element in list: print(counter, element) counter += 1 use enumerate function enumerate function returns iterator with index and element....


How to Join or append a List in python with example

February 28, 2022 ·  1 min read

Suppose you have two lists and output a single list by joining or appending multiple lists. Each element is appended to the end of the new list. Let’s create a list of sub-list or two-dimensional lists in python. one = [1, 2,3,4] two=[5,6,7,8,9] After joining the list, the result is [1, 2, 3, 4, 5, 6,7,8,9] Python joins two lists There are multiple ways to join or append a list...


How to merge two lists with or without duplicates python with example

February 28, 2022 ·  3 min read

This tutorial explains how to concatenate two lists into a single list. Duplicates are allowed. Contents Python merges two lists into a list with duplicates How to merge two lists with unique values How to concatenate an existing list with another list in python? Python merges two lists into a list with duplicates There are multiple ways we can do The below approaches merge the lists into a new list, the order is insertion and iteration order, Duplicates are allowed, No sorting....


How to rename and replace a file in python with example

February 28, 2022 ·  3 min read

Sometimes, We need to rename a file with python code. It means renaming the name of the file and extension. For example, If the file is emp.json, rename emp.json to test.json. Python provides OS modules that contain rename method to rename the file. Python renames a file This short tutorial shows you how to rename with the os.rename() and os.replace() methods use the os.rename() method os.rename() method is used to rename the given file path to the new given path name....


Multiple ways to check key exists or not in Python with examples

February 28, 2022 ·  2 min read

There are multiple for loops in Python to iterate the Key and values of the dictionary. Dictionary in Python contains key and value pairs. Dictionaries are created with and initialized with braces ({}) and pairs of objects are separated by commas. emp = {'id': 1, 'name': 'john', 'salary': 3000} This tutorial shows you multiple ways to check for keys found in Dictionary. The first way is using the in operator that returns True or False, the Second way is using the has_key method, and the third way is using try except to hand key exists or not....


Multiple ways to get random floating number in Python| random uniform function

February 28, 2022 ·  1 min read

This tutorial shows how to generate random floating number in python with code examples. Generate Random Floating number using random uniform function random.uniform() function generates and returns floating number between given numbers. random.uniform(start,end) Generates Unique random number between given numbers(start,end) It returns floating number Here is an example import random result=random.uniform(2.0, 3.0) print(type(result)) print(result) print(round(result)) Output: <class 'float'> 2.6472959042241215 3 ...


Multiple ways to get variable types in Python

February 28, 2022 ·  3 min read

This tutorial explains how to get variable type in python 2. x and Python 3. x. Sometimes, We need to check given variable type is a string. Since Python is a dynamic programming language and variable types can be changed at runtime, It is very useful to print the variable type. Check variable type in Python example There are multiple ways we can check using type() function type() function used to find the type of variable....


Multiple ways to iterate over Dictionary using for loops in Python

February 28, 2022 ·  3 min read

There are multiple for loops in Python to iterate the Key and values of the dictionary. Dictionary in Python contains key and value pairs. Dictionaries are created with and initialized with braces ({}) and pairs of objects are separated by commas. emp = {'id': 1, 'name': 'john', 'salary': 3000} For loop in python provides iteration to iterate key and values. This tutorial explains about How to unpack key and value pairs Iterate through each key and values Loop over dictionary object Loop over tuple of key, value pair in dictionary Multiple ways to iterate dictionary with for loops There are multiple ways to iterate and loop keys and values in Dictionaries...


Multiple ways to remove the empty string from a list of strings in Python

February 28, 2022 ·  1 min read

This tutorial shows remove empty string space from a list in python with code examples For example, Given list is [“one”,“two”,“three”,"",“four”], the output is [“one”,“two”,“three”,“four”] Python to delete an empty string from a list of strings There are multiple ways we can delete an empty string from a list. use filter function filter method iterates the list and accepts None and list, returns the iterator to return list. It is easy and simple, and faster....


Python String split | List of words and characters examples

February 28, 2022 ·  2 min read

This tutorial explains String split into multiple strings and characters To convert a string to a list of characters, the following approaches are used List constructor for in-loop and list comprehension syntax String iterable unpacking operator To convert a string to a list of words, the String split() function is used with a delimiter. Python Convert String to List of characters For example, If the input contains “string”, It is converted to ['s', 't', 'r', 'i', 'n', 'g']...


Python tutorial Examples

February 28, 2022 ·  1 min read

This is a home page of Python tutorials and examples. Python by examples This tutorial contains tutorials and examples grouped with each section Python environment examples Python List examples Check List is empty or not Find First and Last element of an List Get index in for loop iteration List flatten in python example Append multiple Lists in python example Python Date examples Current Date and Time with timezone Python String examples Get Character position in a String Convert List into Single String Convert String into strings and characters Python Dictionary examples For loop Dictionary Iteration Check Key exists in Dictionary Python File examples File size in megabytes ...


Useful Python Environment variable examples in a code

February 28, 2022 ·  3 min read

This tutorial shows you multiple examples in Python Contents How do I see all environment variables in Python? How to check whether the Environment variable exists or not How to set Environment variables in Python How to Check specific environment variable exists? Conclusion How do I see all environment variables in Python? To see all environment variables, Please follow the below steps python os module provides access to all environment variables import the os module into the code call os....


Subscribe
You'll get a notification every time a post gets published here.