Found 10476 Articles for Python

How do I use strings to call functions/methods in Python?

AmitDiwan
Updated on 16-Sep-2022 12:50:43

7K+ Views

Python Functions are generally called using their name. However, you can also use strings to call functions. For that, use the locals() and globals(). Call functions using strings Example In this example, we will learn how to call two functions using strings − def demo1(): print('Demo Function 1') def demo2(): print('Demo Function 2') locals()['demo1']() globals()['demo2']() Output Demo Function 1 Demo Function 2 Call a function using string variable Example In this example, we have created a class Example with the function xyzuvw() that accepts arg and print them. The globals() ... Read More

How do I specify hexadecimal and octal integers in Python?

AmitDiwan
Updated on 16-Sep-2022 12:49:28

10K+ Views

The Hexadecimal and Octal are part of Numeric Types in Python. Let’s see how to specify them one by one. For hexadecimal type, add a preceding 0x. For example − 0x11 For Octal type (base 8), add a preceding 0 (zero). For example − 0O20 Hexadecimal Integers in Python Hexadecimal Number System uses 10 digits and 6 letters, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Letters represent the numbers starting from 10. A = 10. B = 11, C = 12, D = 13, E = 14, F = ... Read More

How do you make a higher order function in Python?

AmitDiwan
Updated on 16-Sep-2022 12:48:24

2K+ Views

A function in Python with another function as an argument or returns a function as an output is called the High order function. Let’s see the propertie − The function can be stored in a variable. The function can be passed as a parameter to another function. The high order functions can be stored in the form of lists, hash tables, etc. Function can be returned from a function. Let’s see some examples − Functions as objects Example The functions are considered as object in this example. Here, the function demo() is assigned to a variable − # ... Read More

What are the rules for local and global variables in Python?

AmitDiwan
Updated on 16-Sep-2022 12:46:41

2K+ Views

Scope of Variables in Python are of two types: local and global. Scope is defined as the accessibility of a variable in a region. Let us first understand both local and global scope before moving towards the rules. Local Scope Example This defines the local scope of a variable i.e. it can be accessed only in the function where it is defined. No access for a variable with local scope outside the function. Let’s see an example − # Variable with local scope can only be access inside the function def example(): i = 5 ... Read More

How do I convert a string to a number in Python?

AmitDiwan
Updated on 16-Sep-2022 12:40:58

940 Views

To convert a string to a number, there are various ways. Let’s see them one by one. Convert a string to number using int() Example In this example, we will convert a string to a number using the int() method − # String to be converted myStr = "200" # Display the string and it's type print("String = ", myStr) print("Type= ", type(myStr)) # Convert the string to integer using int() and display the type myInt = int(myStr) print("Integer = ", myInt) print("Type = ", type(myInt)) Output String = 200 Type= Integer = ... Read More

What are the best Python resources?

AmitDiwan
Updated on 16-Sep-2022 12:38:26

188 Views

Resources for any programming language includes Video Courses, Notes as well as E-books. Here, I will list the best resources of Python. Python Official Documentation A lot of websites are providing Python resources, but the official documentation still works the best. Let’s see the resources provided by them. Beginners Guide To Python − https://wiki.python.org/moin/BeginnersGuide Developer’s Guide To Python − https://devguide.python.org/ Free Python Books − https://wiki.python.org/moin/PythonBooks Python Standard Library − https://docs.python.org/3/library/index.html HOWTOs on Python −https://docs.python.org/3/howto/index.html Latest Talks on Python −https://pyvideo.org/ Python Resources Now, let us talk about the Python Resources except the official documentation. Following are some of ... Read More

What does an object() method do in Python?

AmitDiwan
Updated on 16-Sep-2022 12:36:40

185 Views

To return an empty object, the object() method is used in Python. This acts as a base for all the classes. Let’s see the syntax of object(). No parameter gets included − object() New properties or methods cannot be added to this object. This itself acts as a base for all properties and methods, default for any class. Create an Empty Object Example In this example, we will create an empty object using the object() method − # Create an empty object ob = object() # Display the empty object print("Object = ", ob) Output Object = ... Read More

Why are there separate tuple and list data types in Python?

Sindhura Repala
Updated on 14-Apr-2025 16:41:39

253 Views

Separate tuple and list data types are provided because both have different roles. Tuples are immutable, whereas lists are mutable. That means Lists can be modified, whereas Tuple cannot. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. Let’s see how Lists and Tuples are created. Creating a Basic Tuple We will begin the code by creating a basic tuple with integer elements. Next, specify the topics as advanced concepts like tuples nested within tuples. The provided code initializes a ... Read More

How can I find the methods or attributes of an object in Python?

AmitDiwan
Updated on 16-Sep-2022 12:33:33

2K+ Views

To find the attributes of an object, use the getarr() method in Python. To check if an attribute exist or not, use the hasattr() method. Set an attribute using the setattr() method in Python. Access the attributes of an object Example To access the attributes of an object, we will use the getattr() method in Python − class student: st_name ='Amit' st_age ='18' st_marks = '99' def demo(self): print(self.st_name) print(self.st_age) ... Read More

How can I sort one list by values from another list in Python?

AmitDiwan
Updated on 16-Sep-2022 12:32:06

4K+ Views

We can sort a list by values from another list by setting up the 2nd list as index numbers for the values in the 1st List in sorted order. Sort a List by values from another list Example In this example, we will sort a list by values from another list i.e. the 2nd list will have the index in the order in which they are placed in sorted order − # Two Lists list1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] list2 = [2, 5, 1, 4, 3] print("List1 = ", list1) print("List2 (indexes) = ", list2) # ... Read More

Advertisements