Way back when, in the year 2000, a friend of mine mentioned Python and what a neat language it was. He said I should take a look at it. However, my hands were already full with learning .NET (coming from an ASP background). Of course, I thought nothing could be better than .NET, so why waste my time learning something else? Here we are today and the Python buzz is still going. The language has remained and become more popular. Now I am starting to learn Python and want to share my approach to learning this language. So, let’s start programming with Python.
To start, download Python from http://www.python.org/getit/ . Choose your platform version. I use an Intel 64-based laptop, so the Intel 64 version is for me.
When installed, the Python shell is referred to as IDLE or the Python Console. To bring up the console, do a search for IDLE. All Python files are saved using the .py extension.
To get acclimated to the console, try this print command:
print('This is Python 3.3')
You should see the words This is Python 3.3 printed to the screen. Anything between single or double quotes is considered a string in Python. If you're a Java programmer, you know that only single quotes denote a string, so this is an important distinction for Python.
Creating a variable in Python is easy. You do not need to use a var keyword or typeset designation such as String. You can simply do this:
x = 10
Now, print out the value of x:
You may have also noticed that a semicolon is not necessary to end a statement as it is with most other languages.
Try another variable and then add them together.
y=12 print(x + y)
You can also use other math operators such as -, *, /, and so on. You should also be aware that Python is case-sensitive. The variable X is different from the variable x.
As with most other languages, Python has a set of similar primitive data types such as String, Int (or Integer), Boolean, and Float. When creating a variable, the variable type is determined by the Python complier at runtime. For example, a variable called myType with the value equal to 3.4, would tell Python that the variable is a Float datatype. If the value is just '3', the var is considered an Integer.
To create a script, bring up a text editor by selecting File > New Window from the console. Add the following lines:
x = 10 y = 12 print("The answer is ", x + y)
Before running the script, save it as simpleScipt.py. To run the script, press F5. The console window pops up with the results. You should see the string The answer is 22 in the console window. To add a comment, just use the # operator before the comment line.
In Python, an indent tells the complier that you are in a statement. Unlike other languages that denote a statement using a bracket }; Python uses a colon :. For example, a simple if condition demonstrates this:
x = 10 if x==10: print("x is 10")
Any indented lines below the colon are considered part of the same statement. The next statement starts unindented. If you have programmed with Ruby, this may seem more natural to you, but if not, just remember an indented set of commands is basically the same thing as having those same commands between brackets (as in Java).
One of the most powerful aspects of any language is the ability to manipulate strings. To print out the position of a string character, try adding this to your script editor window:
testVar = "i am a string variable" print (testVar)
You should see the value i printed to your console. You can easily make your variable's string value case-sensitive by using the upper and lower functions:
print (testVar.upper()) or print (testVar.lower())
To capitalize the first word, do the following:
You can use the title() function to capitalize the first letter of each word. Use the strip() function to remove any white space, tabs, or indents from your string.
To get the month, day, and year from the string 05/25/2013, use the split() function:
monthDayYear = date.split('/') print(monthDayYear)
After running the script, you should see ['5', '25', '2013'] printed to the console. Notice how the split function is literally identical to Java's split function. It is used the same way.
To print out the values in a formal manner, use
print("Month: ", monthDayYear) print("Month: ", monthDayYear) print("Month: ", monthDayYear)
Notice how the string array monthDayYear is similar to that of a string array in Java and .NET. You simply access the value by using the index position of the array. The nice thing about string arrays in Python is you do not have to declare them as such. The compiler already knows that when you used the split function and assigned the return value to the variable monthDayYear that this variable was going to be a string array.
You can now start programming with Python when you understand the concepts associated with other programming languages, such as Java. You learned how to write your first script by declaring variables and manipulating strings.
In the next part to this article series, you use statements, loops, and lists in Python. As you can already tell, Python is a fun language to learn.