Nonlocal keyword
Ken Pythonic
by
5y ago
The nonlocal keyword is used to allow an inner function to access variables defined in an outer function. Without using the nonlocal keyword a new variable will be created in the inner function and will have no effect on the outer function's variables. The below example demonstrates this by defining two versions of s. The first s is accessible only to the outer function say_hello and the other s is only accessible to the inner function say_it. def say_hello(): s = "Hello from outer function say_hello" def say_it(): s = "Hello from inner function say_it" print(s) print(s) say ..read more
Visit website
Importing Global Variables in Python
Ken Pythonic
by
5y ago
Global variables in python are actually considered module level variables or module attributesĀ and are not attached to a global namespace. This leads to some confusion when trying to change them from another module. Attempting to change them can lead to some quirky behavior. This is one of the few flaws that I see in an otherwise elegant and well implemented programming language. To demonstrate a common trap in python module attributes make a new file called hello.py and type the code below: g_hello = "Hello World" def print_g_hello(): print(g_hello) hello.py - declaring g_hello global an ..read more
Visit website
Python lambda
Ken Pythonic
by
5y ago
A lambda function in python allows you to write a reusable expression. Unlike saving the result of an expression in a variable the expression is re-evaluated every time it is used.The simplest lambda function in python takes no arguments and simply returns a literal like the one below: x = lambda : 100 print(x()) Notice that when x is declared with the lambda keyword it has to be called by using parenthesis. The above example isn't anymore useful then just assigning x the value of 100. However, the benefit of using lambdas over variables is they are dynamic. That is they will always evaluate ..read more
Visit website
Example - global keyword
Ken Pythonic
by
5y ago
You can declare a global variable in python easily enough but when you try to change it from a function no error is displayed and the old value is still there. #declare a global variable a = 23 def print_a_variable(): print(a) #Printing it works just fine def change_a_variable(): a = 100000 print(a) #This still prints 23 because you can't change it print_a_variable() Output: 23 100000 23 If you use the keyword global in your function then the global is no longer read-only. #declare a global variable a = 23 def print_a_variable(): #global a - not needed here because ..read more
Visit website
The for in Loop In Python
Ken Pythonic
by
5y ago
One of the strongest features, in my opinion, that Python has is its ability to work with sequences. Sequences are ranges, lists, tuples, sets and dictionaries. I think that pythons elegance in working with these sequences is due to the design decision not to include extra syntax. Most languages have two for statements...the for loop and the for in loop. Python only uses the for in loop and has a built in function called range (xrange in python 2.7) to do basic indexing. for i in range(10): print(i) Because python uses zero indexes the above example prints 0-9 and not 1-10. Ranges actually s ..read more
Visit website
Comments in Python
Ken Pythonic
by
5y ago
Single Line Comment Python uses the symbol # to represent a comment. Unlike many languages like C++ and C# there is no begin and end comment. #This is a comment print("Hello World") #This is a comment too You can also put a comment after a line of code but not before a line of code. print("Hello World") #This is a comment too! Python comments are stripped before the code is compiled to byte code for execution so unlike some interpreted languages it does not cause a penalty during runtime. It there a bunch of comments then it could cause extra compile time. A Multiline Comment HackI was qu ..read more
Visit website
If, if..else, if..elif..else
Ken Pythonic
by
5y ago
When you need to execute code if and only if a certain condition is met then you can use the if statement. If statements can be very simple or very complex. ifIn it's simplest form an if statement in python looks like if a == b: <<statement>> if a == b: a = 100 One liner if statements are sometimes pointed at as not pythonic due to readability. But I believe this is unfounded when the statement is very short. Indentation is very important when writing structures such as if statements in python. If we expand the above statement to two lines the if statement only executes lines th ..read more
Visit website
Writing Loops With Break and Continue
Ken Pythonic
by
5y ago
The simplest form of a loop in python is the while True: infinite loop but you can pass other expressions to the while loop that will make it infinite. i = 0 while 1: i += 1 print("Hello "+ str(i)) The above will do the same thing as passing while True. You can write other conditional expressions in a while loop to manipulate the number of repetitions. i = 0 while i <= 10: i += 1 print("Hello "+ str(i)) The above will print hello n 10 times instead of printing forever. The break Statement You can also write infinite loops and break out of them using the break statement in pytho ..read more
Visit website
Python Conditional Expressions
Ken Pythonic
by
5y ago
Python supports conditional expressions. Conditional expressions allow you to execute statements only when a specific condition is met. Conditions are mathematical expressions such as: a == 0 #Is a equal to zero? a != 0 #Is a equal to zero? a > 100 #Is a greater than 100 a >= 100 #Is a greater than or equal to 100 a < c #Is a less than c a <= c #Is a less than or equal to c Conditionals can be used outside of an if, while or other conditional statement. If you set the variable a to a value you can use the above expressions to evaluat ..read more
Visit website
What is Python
Ken Pythonic
by
5y ago
Python is a powerful dynamic, interpreted ( actually byte compiled ) programming language that is extremely useful and widely used. Because of its popularity there is a large amount of amount of documentation, example programs, books, blogs and open source libraries available. Python is also widely used in both commercial and open source projects, and it has the ability to compile to native executable, java, and .NET Although I believe python has a very strong object oriented syntax, my favorite aspect of the language is its ability not to force you in to an object oriented solution. Instead ..read more
Visit website

Follow Ken Pythonic on FeedSpot

Continue with Google
Continue with Apple
OR