Beginners Guide to Programming 4: Coding Basics 1

It has taken some time to make it to this point, but all steps to get here were necessary. We are now going to begin with the basic fundamentals of programming. All examples will be in Python, but I will illustrate how these examples easily move into other programming languages, just change syntax. If you haven't read Steps 2 and 3 please do so now!

First things first if you haven't already done so get to your python enabled command line, and then open vim. I would suggest the following steps:

  1. mkdir Practice ( Create a new clean directory , you will thank me later)

  2. cd Practice

  3. vim practice1.py

You now have a fresh and clean directory and have created/opened a new python file to begin programming in.

STD Out and STD In

Every programming language ,that I know of, has some way to communicate with standard ( std) in and standard (std) out. Std in and out is basically the text you see on the command line ,because our programs will be command line programs this will be important. If you use an IDE like eclipse, your standard output will be sent to a "console", which is a window for viewing a command line. Std in and out are usefull for debugging programs and letting the user know whats happening, even if the user does not need to interact with your program. Let's create a Hello World Program!

First Time Coding and Compiling

In your vim window or other editor type or copy and paste the following:

Print " Hello World!"

In vim then hit escape ":wq " to save and exit vim. You should now be on the command line type " Python Practice.py" . Hello World! should have been displayed to your command line. A common problem some users have is this error "python' is not recognized as an internal or external command, operable program or batch file." If you have this error you need to do the following.

  1. Find install Directory of Python

  2. Type the following into your command line window. For windows "set PATH=<Python install Dir>" for linux/unix export PATH=<Python install dir>

  3. Type python , you should a get weird command line type ctrl z and then enter. You are now good to go.

Next important simple proramming function is stdin, Standard in is how you get input from the user. This is true for any command line program. In python to get input from the user you are going to using the following code.
var = input("Enter Your age: ")
print "you entered ", var

Type this into your text editor, save exit and run. Enter your age, hit enter. Woohoo it works. Note to receive integers from the user you will use input("..."), if you would like to receive text use raw_input("...") . Note that var is a variable and you do not have give it a type. This is convenient, but when you start getting a lot of variables be careful you know what data they are holding. Just to make this clear a variable is something that holds a value for you. All variables must have a different name ( there are exceptions we can talk about later), and in python any variable can contain any type of data. So, var = 2 is just as valid as var = " Monkeys" or var = i. You can keep giving a variable different data if you want, but I wouldn't recommend it.

One of the main functions utilized in any programming languages is arithmetic! Let's take a quick look at arithmetic in general. Most programming languages utillize the well known order of operations. If you don't know the proper order of operations you need to learn it ! Otherwise, programming anything with a math problem will be a nightmare. Learn the order of operations here "Lesson Order of Operations"

Well hopefully you are ready for some arithmetic don't worry we will keep it light! Let's use utilize the old code and add on! In the average langauage you can put arithmetic directly into the std out commands. Try the following. Note: Programming languages have comment, comments are used to make notes in your program and they are ignored when you compile and run your program. To comment in python use # and anything after it will be disregarded.
var = input("Enter Your age: ")
print "you entered ", var
rint 1+3 # A number plus another number
print 3*2 # a number multiplied by another number
print var*2 # our variable var with a number in it multiplied by 2
print var*var # var multipled by var !

If you would like to star a calculation, just set a variable equal to the equation like so.
var = (var * 2)/10
print var

I think you have enough tools to play with for now. Try to get acquainted with getting information in and out to the user, manipulating variables,numbers, print statements, and everything you have learned. You will be surprised what you can do with such simple tools.

As always questions are welcomed and are encouraged, the more you ask the more it helps everyone else. So, Post your Questions, comments, and snide remarks in the comments.

Spread The Love, Share Our Article

Related Posts

No Response to "Beginners Guide to Programming 4: Coding Basics 1"

Post a Comment