Python
- 21 December
Save Object to File in Python
Use pickle.dump() Function To save object to file in Python: Use the open() function with the with keyword to open the specified file. Use the pickle.dump() function to convert the object to a binary format to write it to the file. [crayon-63b887718a7c7977332797/] [crayon-63b887718a7cd729474867/] Python’s pickle is a built-in module for serializing and de-serializing objects (data […]
- 18 December
Write Binary File in Python
Use bytearray() Function To write a binary file in Python: Use the bytearray() function to convert the list of bytes to a bytearray type object. Use a with clause with open() the method in write binary mode(wb) Use write() method to write the byte_array to the file. [crayon-63b887718c48b031187188/] [crayon-63b887718c492498268200/] We created a byte_list using a […]
- 16 December
Convert List to Comma Separated String in Python
Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63b887718ca13164723242/] [crayon-63b887718ca18446623255/] First, we created a list having string-type elements and named that list list_of_strings. Next, we used the […]
- 09 December
Count Occurrences of Character in String in Python
Using count() Method Use String’s count() method to count occurrences of character in String in Python e.g. my_string.count(character). count() method returns number of occurrences of the character in String. [crayon-63b887718ceea606732104/] We will get the following result after running the above program. [crayon-63b887718ceee229786225/] The count() is a conventional method in Python. It holds a substring, start, […]
- 08 December
Find Character in String in Python
Using find() Method To find the character in a string in Python: Use the find() method to find the index of the first occurrence of the supplied character in the input String. Use an if statement to check if the returned index is not -1; if so, print that index; otherwise, print an error. [crayon-63b887718e352255162679/] […]
- 07 December
Get List of Months Between Two Dates in Python
Use pandas.period_range() Method To get the list of months between two specified dates in Python: Use pandas’s period_range() to get list of months between start date and end date with freq='M'. Use list comprehension to iterate over list of months. For each month, use strftime() method to convert it in appropriate format. [crayon-63b887718e8c8860917932/] The execution […]
- 05 December
Remove Substring from String in Python
Use replace() Method To eliminate a substring from string in Python: Define and initiate my_string. Declare substring to remove it from my_string. Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "") [crayon-63b887718e9ec794408242/] The code above will print the following output on the console: [crayon-63b887718e9ef798054523/] We tried removing the substring from my_string. […]
- 04 December
Convert Pandas Dataframe Column to List
Use Series.values.tolist() Method To convert pandas dataframe column to list: Use pd.DataFrame() to read position_salaries as a pandas data frame. Use df["Position"] to get the column position from df Use position.values to get values of the position Use position_values.tolist() to get list of position_values as position_list [crayon-63b887718ef29587540833/] The code above will print the following output […]
- 30 November
Split Integer into Digits in Python
Using List Comprehension To split integer into digits in Python: Use the str() to transform the specified integer to a string. Use a list comprehension to loop over converted String. Use int() to convert every substring to an integer in each iteration. [crayon-63b887718f974619850684/] This code will display the following results. [crayon-63b887718f979122685482/] While using list comprehension […]