CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

11 April, 2022

Python exec() Function

 Programing Coderfunda     April 11, 2022     Python     No comments   

Python exec() Function

The python exec() function is used for the dynamic execution of Python program which can either be a string or object code and it accepts large blocks of code, unlike the eval() function which only accepts a single expression.

Signature

exec(object, globals, locals)

Parameters

object - It should be either string or code object.

globals (optional) - It is used to specify global functions.

locals (optional) - It is used to specify local functions.

Let's see some examples of exec() function which are given below:

Python exec() Function Example 1

This example shows working of exec() function.

  1. x = 5  
  2. exec('print(x==5)')  
  3. exec('print(x+4)')  

Output:

True
9

Python exec() Function Example 2

This example shows exec() dynamic code execution

  1. from math import *  
  2. for l in range(1, 3):  
  3.     func = input("Enter Code Snippet to execute:\n")  
  4.     try:  
  5.         exec(func)  
  6.       except Exception as ex:  
  7.         print(ex)  
  8.         break  
  9. print('Done')  

Output:

Enter Code Snippet to execute:
print(sqrt(16))
4.0
Enter Code Snippet to execute:
print(min(2,1))
1
Done
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Python compile() Function

 Programing Coderfunda     April 11, 2022     Python     No comments   

Python compile() Function

The python compile() function takes source code as input and returns a code object which can later be executed by exec() function.

Signature

compile(source, filename, mode, flag, dont_inherit, optimize)

Parameters

  • source - normal string, a byte string, or an AST (Abstract Syntax Trees) object.
  • filename - File from which the code is read.
  • mode - mode can be either exec or eval or single.
    • eval - if the source is a single expression.
    • exec - if the source is block of statements.
    • single - if the source is single statement.
  • flags and dont_inherit - Default Value= 0. Both are optional parameters. It monitors that which future statements affect the compilation of the source.
  • optimize (optional) - Default value -1. It defines the optimization level of the compiler.

Return

It returns a Python code object.

Let's see some examples of compile() function which are given below:

Python compile() Function Example 1

This example shows to compile a string source to the code object.

  1. # compile string source to code  
  2. code_str = 'x=5\ny=10\nprint("sum =",x+y)'  
  3. code = compile(code_str, 'sum.py', 'exec')  
  4. print(type(code))  
  5. exec(code)  
  6. exec(x)  

Output:

<class 'code'>
sum = 15

Python compile() Function Example 2

This example shows to read a code from the file and compile.

Let's say we have mycode.py file with following content.

  1. x = 10  
  2. y = 20  
  3. print('Multiplication = ', x * y)  

We can read this file content as a string ,compile it to code object and execute it.

  1. # reading code from a file  
  2. f = open('my_code.py', 'r')  
  3. code_str = f.read()  
  4. f.close()  
  5. code = compile(code_str, 'my_code.py', 'exec')  
  6. exec(code)  

Output:

Multiplication =200
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Python callable() Function

 Programing Coderfunda     April 11, 2022     Python     No comments   

Python callable() Function

A python callable() function in Python is something that can be called. This built-in function checks and returns True if the object passed appears to be callable, otherwise False.

Signature

callable(object)

Parameters

object - The object that you want to test and check, it is callable or not.

Return

Returns True if the object is callable, otherwise False.

Let's see some examples of callable() function.

Python callable() Function Example 1

Check if a function is callable:

  1. def x():  
  2.  a = 5  
  3.   
  4. print(callable(x))  

Output:

True

Python callable() Function Example 2

Check if a function is not callable (such as normal variable).

  1. x = 5  
  2. print(callable(x))  

Output:

False
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Python bytes() Function

 Programing Coderfunda     April 11, 2022     Python     No comments   

Python bytes() Function

The python bytes() function in Python is used for returning a bytes object. It is an immutable version of bytearray() function.

It can create empty bytes object of the specified size.

Signature

  • bytes(source)
  • bytes(encoding)
  • bytes(error)

Parameters

source is used to initialize the bytes object. It is an optional parameter.

encoding is optional unless source is string type. It is used to convert the string to bytes using str.encode() function

errors is also an optional parameter. It is used when the source is string type. Also, when encoding fails due to some error.

Return

It returns a bytes object.

Let's see some examples of bytes() function to understand it's functionality.

Python bytes() Function Example 1

This is a simple example of converting string into bytes.

  1. string = "Hello World."  
  2. arr = bytes(string, 'utf-8')  
  3. print(arr)  

Output:

b ' Hello World.'

Python bytes() Function Example 2

This example creates a byte of a given integer size.

  1. size = 5  
  2. arr = bytes(size)  
  3. print(arr)  

Output:

b'\x00\x00\x00\x00\x00'

Python bytes() Function Example 3

This example converts iterable list to bytes.

  1. List = [1, 2, 3, 4, 5]  
  2. arr = bytes(List)  
  3. print(arr)  

Output:

b'\x01\x02\x03\x04\x05'
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Python bool() Function

 Programing Coderfunda     April 11, 2022     Python     No comments   

Python bool() Function

The python bool() method converts value to boolean (True or False) using the standard truth testing procedure.

Signature

  1. bool([value])  

Parameters

It is not mandatory to pass value to bool(). If you do not pass the value, bool() returns False.

In general , bool() takes a single parameter value.

Return

The bool() returns:

  • False if the value is omitted or false
  • True if the value is true

Python bool() Function Example

  1. test = []  
  2. print(test,'is',bool(test))  
  3.   
  4. test = [0]  
  5. print(test,'is',bool(test))  
  6.   
  7. test = 0.0  
  8. print(test,'is',bool(test))  
  9.   
  10. test = None  
  11. print(test,'is',bool(test))  
  12.   
  13. test = True  
  14. print(test,'is',bool(test))  
  15.   
  16. test = 'Easy string'  
  17. print(test,'is',bool(test))  

Output:

[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Python bin() Function

 Programing Coderfunda     April 11, 2022     Python     No comments   

Python bin() Function

The python bin() function is used to return the binary representation of a specified integer. A result always starts with the prefix 0b.

Signature

  • bin(n)

Parameters

  • n - An integer

Return

It returns the binary representation of a specified integer.

Python bin() Function Example 1

  1. x =  10  
  2. y =  bin(x)  
  3. print (y)  

Output:

0b1010

Note: The result will always be prefixed with '0b'.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Python all() Function

 Programing Coderfunda     April 11, 2022     Python     No comments   

Python all() Function

The python all() function accepts an iterable object (such as list,dictionary etc.). It returns True if all items in passed iterable are true, otherwise it returns False. If the iterable object is empty, the all() function returns True.

Signature

  1. all (iterable)  

Parameters

  • iterable - The objects which contain the elements i.e. list, tuple and dictionary, etc.

Return

  • True - If all the elements in an iterable are true.
  • False - If all the elements in an iterable are false..

Python all() Function Example 1

Let's see how all() works for lists?

  1. # all values true  
  2. k = [1, 3, 4, 5]  
  3. print(all(k))  
  4.   
  5. # all values false  
  6. k = [0, False]  
  7. print(all(k))  
  8.   
  9. # one false value  
  10. k = [1, 3, 4, 0]  
  11. print(all(k))  
  12.   
  13. # one true value  
  14. k = [0, False, 5]  
  15. print(all(k))  
  16.   
  17. # empty iterable  
  18. k = []  
  19. print(all(k))  

Output:

True
False
False
False
True

Python all() Function Example 2

The below example shows how all() works for dictionaries.

  1. # Both the keys are true  
  2. dict1 = {1: 'True', 2: 'False'}  
  3. print(all(dict1))  
  4.   
  5. # One of the key is false  
  6. dict2 = {0: 'True', 1: 'True'}  
  7. print(all(dict2))  
  8.   
  9. # Both the keys are false  
  10. dict3 = {0: 'True', False: 0}  
  11. print(all(dict3))  
  12.   
  13. # Empty dictionary  
  14. dict4 = {}  
  15. print(all(dict4))  
  16.   
  17. # Here the key is actually true because  
  18. #  0 is non-null string rather than a zero  
  19. dict5 = {'0': 'True'}  
  20. print(all(dict5))  

Output:

True
False 
False 
True 
True

Python all() Function Example 3

The below example shows how all() works for tuples.

  1. # all true values  
  2. t1 = (1, 2, 3, 4, 5)  
  3. print(all(t1))  
  4.   
  5. # one false value  
  6. t2 = (0, 1, "Hello")  
  7. print(all(t2))  
  8.   
  9. # all false values  
  10. t3 = (0, False , 0)  
  11. print(all(t3))  
  12.   
  13. # one true value, all false  
  14. t4 = (True, 0, False)  
  15. print(all(t4))  

Output:

True 
False 
False 
False
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Older Posts Home

Meta

Popular Posts

  • Vue.js Tutorial
      Vue.js Installation Compatibility Check Before going to install and use Vue.js in your project, you should check the compatibility issues....
  • JqueryUI Tutorial
    JqueryUI Tutorial    JqueryUI is the most popular front end frameworks currently. It is sleek, intuitive, and powerful mobile first fr...
  • Laravel - Application Structure
    The application structure in Laravel is basically the structure of folders, sub-folders and files included in a project. Once we create a ...
  • Python Tutorial
      Python   is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van...
  • CSS Online Training
    CSS Online Training CSS is used to control the style of a web document in a simple and easy way. CSS is the acronym for "Casca...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda