- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 35163 Articles for Programming
1K+ Views
Python IdentifiersIdentifier is the name given to entities like class, functions, variables etc. in Python. It helps in knowing one entity from another.Rules for writing identifiersIdentifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_3 and print_to_screen, all are valid examples.An identifier cannot start with a digit. 2variable is invalid, but variable2 is perfectly correct.Keywords cannot be used as identifiers. The word ‘global’ is a keyword in python. So we get an invalid syntax error hereExampleglobal = "syntex" print globalOutputFile ... Read More
239 Views
We write a unittest that fails only if a function doesn't throw an expected exception.We also test if a Python function throws an exception.For example, see the sample code we paste into the Python shell to test Python's type-safety:Exampleimport unittest class MyTestCase(unittest.TestCase): def test_1_cannot_add_int_and_str(self): with self.assertRaises(TypeError): 1 + '1' def test_2_cannot_add_int_and_str(self): import operator self.assertRaises(TypeError, operator.add, 1, '1') unittest.main(exit=False)Running the testsOutputAnd the terminal outputs the following − .. ---------------------------------------------------------------------- Ran 2 tests in 0.001s OKTest one uses assertRaises as a context ... Read More
317 Views
It is found that if python code is run normally and then if it is run in a python function, it runs faster in the latter case. I want to know why python code runs faster in a function.It is generally found that it is faster to store local variables than global variables in a python function. This can be explained as under.Aside from local/global variable store times, opcode prediction makes the function faster.CPython is the original Python implementation we download from Python.org. It is called CPython to distinguish it from later Python implementations, and to distinguish the implementation of ... Read More
334 Views
Variables are classified into Global variables and Local variables based on their scope. The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined. Local variables are those that are defined inside a function but have a scope that is only applicable to that function, as opposed to global variables, which are defined outside of any function and have a global scope. In other words, we may argue that although global variables are accessible ... Read More
255 Views
The Python libraries are now available in MATLAB (since 2014b). We can run code directly in MATLAB if we are using version 2014b or later.This makes it possible to use python modules in MATLAB. Without any other changes, just prefix ‘py’ before the python library name you want to use. Let us use the Python calendar module as an example.py.calendar.isleap(2016); py.calendar.isleap(2017);OUTPUTans =1 ans = 0To run our own function, we can create a file in our current MATLAB working directory. Let’s say we created a file called ‘hello.py’ that contains these two lines:def world(): return 'hello world';# In ... Read More
143 Views
In the following code, we import sqlite3 module and establish a database connection. We create a table and then insert data and retrieve information from the sqlite3 database and close the connection finally.Example#sqlitedemo.py import sqlite3 from employee import employee conn = sqlite3.connect('employee.db') c=conn.cursor() c.execute(‘’’CREATE TABLE employee(first text, last text, pay integer)’’’) emp_1 = employee('John', 'Doe', 50000 ) emp_2 = employee('Jane', 'Doe', 60000) emp_3 = employee('James', 'Dell', 80000) c.execute(‘’’INSERT INTO employee VALUES(:first, :last, :pay)’’’, {'first':emp_1.first, 'last':emp_1.last, 'pay':emp_1.pay}) c.execute(‘’’INSERT INTO employee VALUES(:first, :last, :pay)’’’, {'first':emp_2.first, 'last':emp_2.last, 'pay':emp_2.pay}) c.execute(‘’’INSERT INTO employee VALUES(:first, :last, :pay)’’’, {'first':emp_3.first, 'last':emp_3.last, 'pay':emp_3.pay}) c.execute("SELECT * FROM employee WHERE ... Read More
8K+ Views
We have this code below which will the given json object as a parameter to a python function.exampleimport json json_string = '{"name":"Rupert", "age": 25, "desig":"developer"}' print type (json_string) def func(strng): a =json.loads(strng) print type(a) for k,v in a.iteritems(): print k,v print dict(a) func(json_string) Output desig developer age 25 name Rupert {u'desig': u'developer', u'age': 25, u'name': u'Rupert'}
1K+ Views
We return a json object from a python function as follows using given python dictionary.Exampleimport json a = {'name':'Sarah', 'age': 24, 'isEmployed': True } # a python dictionary def retjson(): python2json = json.dumps(a) print python2json retjson()Output{"age": 24, "isEmployed": true, "name": "Sarah"}
2K+ Views
The handling of white spaces between strings is managed effectively in Python. In this article, we'll examine the methods to provide a space in the string when we are unsure of how much space is necessary. Using string expandtabs() method A built-in method in Python called String expandtabs() is used to handle spaces in strings. The expandtabs() method in Python creates a copy of the string that has all tab characters '\t' up to the next multiple tabsize parameters replaced with whitespace characters. There may be instances when it is necessary to define how much space should be left in ... Read More
82 Views
A function is a callable object in Python, i.e. can be called using the call operator. However other objects can also emulate a function by implementing __call__method. exampledef a(): pass # a() is an example of function print a print type(a)OutputC:/Users/TutorialsPoint/~.py A method is a special class of function, one that can be bound or unbound.Exampleclass C: def c(self): pass print C.c # example of unbound method print type(C.c) print C().c # example of bound method print type(C().c) print C.c()Of course, an unbound method cannot be called without passing as argument.Output ... Read More
To Continue Learning Please Login
Login with Google