What is the use of "assert" statement in Python?

Manogna
Updated on 27-Sep-2019 08:04:22

179 Views

The assert statement has the following syntax.assert , The line above is read as: If evaluates to False, an exception is raised and will be output.If we want to test some code block or an expression we put it after an assert keyword. If the test passes or the expression evaluates to true nothing happens. But if the test fails or the expression evaluates to false, an AssertionError is raised and the message is printed out or evaluated.Assert statement is used for catching/testing user-defined constraints. It is used for debugging code and is inserted at the start of ... Read More

Check if a large number is divisible by 3 or not in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:04:05

346 Views

Here we will see how to check a number is divisible by 3 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 3, if the sum of digits is divisible by 3.Example Live Demo#include using namespace std; bool isDiv3(string num){    int n = num.length();    long sum = accumulate(begin(num), end(num), 0) - '0' * n;    if(sum % 3 == 0)       return true;       return false; } int main() {    string num = "3635883959606670431112222";    if(isDiv3(num)){       cout

Python program to print all even numbers in a range

Pavitra
Updated on 27-Sep-2019 08:01:48

1K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a range, we need to print all the even numbers in the given range.The brute-force approach is discussed below −Here we apply a range-based for loop which provides all the integers available in the input interval.After this, a check condition for even numbers is applied to filter all the odd numbers.This approach takes O(n) + constant time of comparison.Now let’s see the implementation below −Examplestart, end = 10, 29 # iteration for num in range(start, end + 1):    # check   ... Read More

Get all the tables from a MySQL database having a specific column, let’s say xyz?

AmitDiwan
Updated on 27-Sep-2019 08:00:45

139 Views

Let’s say we have a database “web” and we need to get all the tables having a specific column ’StudentFirstName’.For this, below is the query −mysql> select myColumnName.table_name from information_schema.columns myColumnName where myColumnName.column_name = 'StudentFirstName' and table_schema='web';This will produce the following output −+---------------+ | TABLE_NAME | +---------------+ | demotable109 | | demotable297 | | demotable335 | | demotable395 | | demotable418 | | demotable425 | | demotable436 | +---------------+ 7 rows in set (0.14 sec)Therefore, the above tables have one of the column names as “StudentFirstName”.Let us check the description ... Read More

How to raise Python exception from a C extension?

Rajendra Dharmkar
Updated on 27-Sep-2019 07:58:58

119 Views

For the above module, we need to prepare following setup.py script −from distutils.core import setup, Extension setup(name='helloworld', version='1.0', \ ext_modules=[Extension('helloworld', ['hello.c'])])Now, we use the following command,$ python setup.py installOnce we install the extension, we would be able to import and call that extension in our Python script test.py and catch the exception in it as follows −#test.py import helloworld try: print helloworld.helloworld() except Exception as e: print str(e)This would produce the following result −bad format char passed to Py_BuildValue

Convert MySQL timestamp to UNIX Timestamp?

AmitDiwan
Updated on 27-Sep-2019 07:58:58

964 Views

To convert MySQL timestamp to UNIX Timestamp, use the UNIX_TIMESTAMP(). Following is the syntax −select unix_timestamp(yourColumnName) from yourTableName;Let us first create a table −mysql> create table DemoTable(    Duetimestamp timestamp ); Query OK, 0 rows affected (2.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(now()); Query OK, 1 row affected (1.53 sec) mysql> insert into DemoTable values('2016-01-21 12:34:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-05-01 02:00:00'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable values('2017-03-02 01:10:20'); Query OK, 1 row affected (10.19 sec)Display all records from ... Read More

Check if a large number is divisible by 25 or not in C++

Arnab Chakraborty
Updated on 27-Sep-2019 07:58:01

134 Views

Here we will see how to check a number is divisible by 25 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 25, when the last two digits are 00, or they are divisible by 25.Example Live Demo#include using namespace std; bool isDiv25(string num){    int n = num.length();    int last_two_digit_val = (num[n-2] - '0') * 10 + ((num[n-1] - '0'));    if(last_two_digit_val % 25 == 0)       return true;       return false; } int main() {    string num = "451851549333150";    if(isDiv25(num)){       cout

How can I replace & with an ampersand in my MySQL database?

AmitDiwan
Updated on 27-Sep-2019 07:57:41

548 Views

To replace & with an ampersand, use MySQL REPLACE(). Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value varchar(100) ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values('@amp'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(Value) values('&'); Query OK, 1 row affected (1.09 sec) mysql> insert into DemoTable(Value) values('#amp'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----+-------+ ... Read More

Find average on the basis of corresponding duplicate VARCHAR values in MySQL

AmitDiwan
Updated on 27-Sep-2019 07:56:21

60 Views

Let us first create a table −mysql> create table DemoTable(    Value int,    Value2 varchar(100) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, '999.999.999.999'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20, '888.888.888.888'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(30, '999.999.999.999'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+-----------------+ | Value | Value2 ... Read More

Python Program to find whether a no is the power of two

Pavitra
Updated on 27-Sep-2019 07:56:11

137 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a number n, we need to check whether the given number is a power of two.ApproachContinue dividing the input number by two, i.e, = n/2 iteratively.We will check In each iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2.If n becomes 1 then it is a power of 2.Let’s see the implementation below −Exampledef isPowerOfTwo(n):    if (n == 0):       return False    while (n != 1):       ... Read More

Advertisements