Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, June 10, 2019

The While Loop in Python

In order to perform operations over and over again in a programming language you need to have syntax that allows you to execute the same lines of code without having to re-write each individual line. In python there are a few ways to do this and one way is the while command.

The indentation is important in python. All statements under the while command must be indented by at least one space, and by convention some programmers use 4 spaces. If you use an editor the tab key must be setup in the editor so that when you press tab it really insert spaces instead of tabs.


The below example will print Hello n until the end of time or you press CTRL-C.

i = 0
while True:
 i += 1
 print("Hello "+ str(i))


This is what we refer to as an infinite loop and when I learned programming 20 years ago we were told to avoid them at all cost because it would hang your computer and cause you to lose your work. But python, and modern operating systems for that matter, are very good and handling this situation.

The output for the above program is as follows:


Hello 2548067
Hello 2548068
Hello 2548069
Hello 2548070
Hello 2548071
Hello 2548072
Hello 2548073
Hello 2548074
Hello 2548075
Hello 2548076
Hello 2548077
Hello 2548078
^CHello 2548079
Hello 2548080
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
KeyboardInterrupt

You will notice ˆC and the KeyboardInterrupt in the output. This is expected behavior because we never gave a command to end the loop.

Also understand that inside of the parenthesis we used "Hello " + str(i). This is called string concatenation. We use the plus operator, which you would expect to be used for math operations, to join two strings together. The str(i) function is used to convert i, which is an integer, to a string instead.

For a dynamic language python can be strict on using types. Many dynamic languages would automatically assume that you wanted i to be converted to a string. Python simply wants to make sure that you really intended on converting i to a string before it joins it to "Hello".


https://www.w3schools.com/python/python_while_loops.asp - More information about while loops and for loops.

I am writing this blog and testing my examples using an iMac very similar to this one. You don't really need a powerful computer but its nice to have one that has a nice display and is very quite. Macintoshes are very elegant and the unix base that OS X is built on helps me to access unix command line tools which I find more powerful than Windows command prompts.




Reading User Input in Python

Reading Input From The Keyboard

In order to process user input you are going to need to retrieve keyboard data from the user. One of the most easy and straightforward ways of getting user input is to use the input function.


>>> a = input("Enter a number:")
Enter a number:23
>>> print(a)
23
>>> 

In the above example we use the input command to retrieve a number and store it in the variable a. We could have first used the print command and then called input() but it is more compact and easier to read to display the user input and retrieve it at the same time.

A common mistake would be to call input(a) expecting the input command to store the value in a.

>>> input(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> 

NOTE: The variable a is automatically declared in python and we don't even have to give it an initial value before retrieving it from the user.



Sunday, June 9, 2019

What Does It Mean To Be Pythonic?

As you go out into the community and seek help from the python developers
you will encounter various, well meaning and experienced python developers that will accuse you of not being "pythonic". This simply means that you are not writing code the way the python community believes is efficient, straightforward and follows python guidelines.

The above T-Shirt could help you if you are feeling a little bit code shamed. To be honest I know too many programming languages to really be cleansed by the python community.

To protect me from such religious zealots I would like to reference the below definition of pythonic until I write better code :)




pythonic

1
[ pahy-thon-ik, pi- ]





adjective

of or relating to pythons.
similar to a python; pythonlike.
gigantic or monstrous.

https://www.dictionary.com/browse/pythonic





Variables in Python

In order to do anything with your output it is useful, and it can be argued absolutely necessary to store information somehow in order to manipulate that information. Python has a very straight forward and easy syntax ( set of rules ) in order to do this called variables.

They are called variables because the value can be changed.

To declare a variable in python is simple:
1
2
a = 1
b = 100

In the above example I have declared two variables named a and b. I store the value 1 in a and the value 100 in b. To avoid confusion I like to say that a becomes 1 and b becomes 100. This is to avoid confusion between equality and assignment. To say that a is equal 1 and/or b = 100 could be true for this portion of the code but the variable could later be manipulated to become another value.

Now that these values are stored in variables I can output them using the print command we learned earlier:

1
2
3
4
5
a = 1
b = 100

print(a)
print(b)

If you type this in to a python shell or in a python program you will get the output:

 1
 100


Continued On -> More About Variables in Python


https://www.w3schools.com/python/python_variables.asp - More about Variables

Python Programming: A Practical Introduction To Python Programming For Total Beginners

Note: I use the term declare to describe how to create a variable in python. If I am honest I have told a white lie. To declare a variable usually means that it has to be predefined before it is used. In python its first use automatically declares it. For someone learning python I think this is splitting hairs and it is inevitable that programmers are going to ask "How do I declare a variable in python" and the python zealots will respond with "One does not declare variables in python".

nonlocal keyword

The nonlocal keyword is used to allow an inner function to access variables defined in an outer function. Without using the nonlocal keywo...