Programming Articles - Page 595 of 3363

How do I get int literal attribute instead of SyntaxError in Python?

AmitDiwan
Updated on 19-Sep-2022 13:32:12

331 Views

To get int literal attribute instead of SyntaxError, use a space or parenthesis. The int literal is a part if Numeric Literals in Python. Numeric Literals also includes the following four different numerical types − int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point. long (long integers ) − Also called longs, they are integers of unlimited size, written like integers and followed by an uppercase or lowercase L. float (floating point real values) − Also called floats, they represent real numbers and are written with ... Read More

Why does -22 // 10 return -3 in Python?

AmitDiwan
Updated on 19-Sep-2022 13:30:57

429 Views

In Python, -22//10 returns -3 because of the concept of Floor Division i.e. Double Slash Operator. The // is the Double Slash i.e. Arithmetic Operator. Let us first understand about it. Floor Division in Python The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity). In Python, the // is the double slash operator i.e. Floor Divison. The // operator is used to perform division that rounds the result down ... Read More

How it is possible to write obfuscated oneliners in Python?

AmitDiwan
Updated on 19-Sep-2022 13:28:41

403 Views

Yes, it is possible to write obfuscated one-liners in Python using Lambda. Before moving further, let us first understand what are Lambdas in Python. Python Lambda The Lambda expressions allow defining anonymous functions. A lambda function is an anonymous function i.e. a function without a name. Let us see the syntax − lambda arguments: expressions The keyword lambda defines a lambda function. A lambda expression contains one or more arguments, but it can have only one expression. Example Let us see an example − myStr = "Thisisit!" (lambda myStr : print(myStr))(myStr) Output Thisisit! Merge Elements in a ... Read More

Is there an equivalent of C’s “?:” ternary operator in Python?

AmitDiwan
Updated on 19-Sep-2022 13:26:25

501 Views

Yes, we can work around C Language’s ternary operator in Python as well i.e. a similar way do exist. Let us first see an example of C Language’s ternary operator − Example #include int main() { int x = 10; int y; y = (x == 1) ? 20: 30; printf( "Value of y = %d", y ); y = (x == 10) ? 20: 30; printf( "Value of y = %d", y ); } Output Value ... Read More

My Python program is too slow. How do I speed it up?

AmitDiwan
Updated on 19-Sep-2022 13:09:17

347 Views

If your Python program is too slow, you can follow the below given tips and tricks − Abstraction Avoid excessive abstraction, especially under the form of tiny functions or method. Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower Avoid Looping Overhead If the body of your loop is simple, the interpreter overhead of the for loop itself can be a substantial amount of the overhead. This is where the map function works in a better way. The only restriction ... Read More

Why did changing list ‘y’ also change list ‘x’ in Python?

AmitDiwan
Updated on 19-Sep-2022 12:36:08

296 Views

Example In this article, we will see if you will change a list, let’s say List y will also change list x. For this, let us first see an example with two lists and try to append() and print − x = [] y = x print("Value of y = ", y) print("Value of x = ", x) y.append(25) print("After changing...") print("Value of y = ", y) print("Value of x = ", x) Output ('Value of y = ', []) ('Value of x = ', []) After changing... ('Value of y = ', [25]) ('Value of x ... Read More

What is the difference between arguments and parameters in Python?

AmitDiwan
Updated on 19-Sep-2022 12:33:48

2K+ Views

The concept of arguments and parameters are part of Functions in Python. Therefore, before moving further let us learn how to create a function and parameterised function. A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Create a Function Example Let us create a basic function − # Define a function def sample(): print("Inside a Function") # Function call sample() Output Inside a Function Create a Parameterized Function Here, we are ... Read More

How can I pass optional or keyword parameters from one function to another in Python?

AmitDiwan
Updated on 19-Sep-2022 12:29:10

1K+ Views

To pass optional or keyword parameters from one function to another, collect the arguments using the * and ** specifiers in the function’s parameter list But, at first, do know what are *args and **args in Python. Let us understand them − Variable-length/ Arbitrary arguments in Python (*args) Example When you don’t know in advance about the number of arguments to be passed, the arguments are variable-length. Include an asterisk i.e. * before the parameter name while defining the function. Let us see an example: def demo(*car): print("Car 1 = ", car[0]) print("Car 2 ... Read More

Why are default values shared between objects in Python?

AmitDiwan
Updated on 19-Sep-2022 12:24:28

2K+ Views

The default values concept in Python are based on using mutable or immutable objects. It is good programming practice to not use mutable objects as default values. Instead, use None as the default value to avoid issues. Immutable objects such as numbers, strings, tuples, and None, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion. Let’s see the example of a Dictionary in a function and the issues with it and how to fix it. Problem We have a function. In that we have a Dictionary as a parameter with ... Read More

What are the “best practices” for using import in a Python module?

AmitDiwan
Updated on 19-Sep-2022 12:19:22

2K+ Views

The import statement, just like any other statement or keyword in Python should be used and added to the code properly following the best practices. Let’s see them one by on − Multiple Imports Multiple Imports should usually be on separate lines. For example − import numpy import pandas import matplotlib Always on the Top Imports are always put at the top of the file i.e. After any module comments and docstrings Before module globals and constants. For example − # import the numpy module import numpy Import Modules in an Order A good practice is ... Read More

Advertisements