How to import a single function from a Python module?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:30:55

9K+ Views

You can use the "from module import function" statement to import a specific function from a Python module. For example, if you want to import the sin function from the math library without importing any other function, you can do it as follows:>>> from math import sin >>> sin(0) 0.0Note that you don't have to prefix sin with "math." as only sin has been imported and not math. Also you can alias imported functions. For example,>>> from math import cos as cosine >>> cosine(0) 1.0

Get the count of two table fields in a single MySQL query?

AmitDiwan
Updated on 30-Sep-2019 08:28:18

545 Views

For this, you can use the CASE statement along with SUM(). Here, we will be finding the count of Male and Female records from a column with employee gender values. Let us first create a table −mysql> create table DemoTable (    EmployeeGender ENUM('Male', 'Female') ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Male'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Female'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Male'); Query OK, 1 row affected (0.10 sec) mysql> insert into ... Read More

What is the usage of “@” symbol in MySQL stored procedure?

AmitDiwan
Updated on 30-Sep-2019 08:26:39

731 Views

The @ symbol in a stored procedure can be used for user-defined session variables. Let us first create a table −mysql> create table DemoTable (    StudentName varchar(50) ); Query OK, 0 rows affected (1.30 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (1.00 sec) mysql> insert into DemoTable values('John Doe'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.53 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

How to replace the last occurrence of an expression in a string in Python?

Manogna
Updated on 30-Sep-2019 08:25:24

4K+ Views

This problem can be solved by reversing the string, reversing the string to be replaced, replacing the string with reverse of string to be replaced with and finally reversing the string to get the result. You can reverse strings by simple slicing notation - [::-1]. To replace the string you can use str.replace(old, new, count). For example,  def rreplace(s, old, new):     return (s[::-1].replace(old[::-1], new[::-1], 1))[::-1]  rreplace('Helloworld, hello world, hello world', 'hello', 'hi') This will give the output:'Hello world, hello world, hi world'Another method by which you can do this is to reverse split the string once on the old string ... Read More

Fetch records on the basis of LastName using MySQL IN()

AmitDiwan
Updated on 30-Sep-2019 08:25:09

88 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 'Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('John', 'Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris', 'Brown'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Robert', 'Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

How to correctly sort a string with a number inside in Python?

Manogna
Updated on 30-Sep-2019 08:24:05

2K+ Views

This type of sort in which you want to sort on the basis of numbers within string is called natural sort or human sort. For example, if you have the text:['Hello1', 'Hello12', 'Hello29', 'Hello2', 'Hello17', 'Hello25'] Then you want the sorted list to be:['Hello1', 'Hello2', 'Hello12', 'Hello17', 'Hello25', 'Hello29'] and not:['Hello1', 'Hello12', 'Hello17', 'Hello2', 'Hello25', 'Hello29'] To do this we can use the extra parameter that sort() uses. This is a function that is called to calculate the key from the entry in the list. We use regex to extract the number from the string and sort on both text and number.  import re ... Read More

How to replace \\ with in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:23:04

579 Views

There are two ways to go about replacing \ with \ or unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:>>> import ast >>> a = '"Hello,world"' >>> print ast.literal_eval(a) Hello, worldAnother way is to use the decode('string_escape') method from the string class. For example,>>> print "Hello,world".decode('string_escape') Hello, world

MySQL query to replace only the NULL values from the table?

AmitDiwan
Updated on 30-Sep-2019 08:22:56

82 Views

For this, you can use the property IS NULL for null values in MySQL. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 ... Read More

Display the record with non-duplicate Id using MySQL GROUP BY and HAVING

AmitDiwan
Updated on 30-Sep-2019 08:19:40

111 Views

Let us first create a table −mysql> create table DemoTable (    Id int,    ColorName varchar(100) ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Red'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(101, 'Green'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(101, 'Blue'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(102, 'Yellow'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(100, 'Purple'); Query OK, 1 row affected (0.12 sec) mysql> insert ... Read More

Delete last value and fix two new values (VARCHAR Numbers) in MySQL declared as VARCHAR?

AmitDiwan
Updated on 30-Sep-2019 08:16:41

64 Views

Let us first create a table. Here, we have VARCHAR type for value −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (1.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('100'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('1244'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('15789'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+ | Value | +-------+ | 100 | ... Read More

Advertisements