There really is only one way to learn to program. You must read code and write code (as computer programs are often called). I’m going to show you lots of code. You should type in code that I show you to see what happens. Play around with it and make changes. The worst that can happen is that it won’t work. SO Dont Be Afraid to Code !!!
##Python is easy to learn >>> Hello, World! |
Creating and Running Programs
Go into IDLE and in the menu at the top, select File then New Window. In the new window that appears, type the following :
print (“Hello, World!")
Now save the program: select File from the menu, then Save. Save it as “hello.py" (you can save it in any folder you want). Now that it is saved it can be run. Next run the program by going to Run then Run Module.
It is very useful to stick to some rules regarding the file names of Python programs. Otherwise some things might go wrong unexpectedly. These don’t matter as much for programs, but you can have weird problems if you don’t follow them.
1 Always save the program with the extension .py. Do not put another dot somewhere else in the file name.
2 Only use standard characters for file names: letters, numbers, dash (-) and underscore (_).
3 White space (“") should not be used at all (use e.g. underscores instead).
4 Do not use anything other than a letter (particularly no numbers!) at the beginning of a file name.
5 Do not use “non-english" characters (such as ä, ö, ü, å or ß) in your file names—or, even better, do not use them at all when programming.
What we were doing above was using a command called print. The print command is followed by one or more arguments. So in this example
print(“Hello, World!")
There is one argument, which is “Hello, World!". Note that this argument is a group of characters enclosed in double quotes (“). This is commonly referred to as a string of characters, or string, for short. Another example of a string is “Twinkle Twinkle Little Star".
A command and its arguments are collectively referred to as a statement, so print “Hello, World!" is an example of a statement.
Let’s type our first program on Pycharm or any IDE that you are working on. We are using print(), a built-in python function. This function lets you print the output on the screen.
print(“2 + 2 is", 2 + 2)
Output : 2 + 2 is 4 3 * 4 is 12 100 – 1 is 99 (33 + 2) / 5 + 11.5 is 18.5 |
The first argument is the string “2 + 2 is" and the second argument is the mathematical expression 2 + 2, which is commonly referred to as an expression.
What is important to note is that a string is printed as is (the string is what is within the double quotes but doesn’t include the double quotes themselves. So the string is printed without the enclosing double quotes.) But an expression is evaluated, (in other words, converted) to its actual value.
By default, print() creates newline at the end of the statement. In the previous examples, we saw that the each output of the print() was printed in the newline. Now, let’s say we don’t want the content of the second print() function to be printed in the newline, instead we want to display in the first line itself. In that case, we need to replace the unseen newline character with a blank space or could even be nothing – “”
Let’s look at below example:
print(“Jack and Jill went up a hill", end=“")
Output : Jack and Jill went up a hillto fetch a pail of water; Jack fell down, and broke his crown,and Jill came tumbling after. |
The end keyword replaces the unseen newline character. The end replaces the newline value with whatever we want to print. In the above example, we have set no value to end hence nothing was printed between the two lines of text.
Often in programming you are doing something complicated and may not in the future remember what you did. It’s a good idea to put your thoughts right there with the code so that anyone who gets to work on it later, knows why a certain logic is implemented in a way it is done. A comment is a note to you and other programmers explaining what is happening. For example :
# This is a comment |
To comment continous lines of code, there is an easier way to do it. We can comment using ‘’’ as shown in example below:
"’ |
Use as many useful comments as you can in your program to:
• explain assumptions
• explain important decisions
• explain important details
• explain problems you’re trying to solve
• explain problems you’re trying to overcome in your program, etc.
In the programs to follow, you will see that I have used comments extensively, many times, the explanation can also be found within the program, so read the programs carefully. I believe practice and only practice can make us perfect so do practice programs with me.
1. Write a program that prints your full name and your birthday as separate strings.
2. Write a program that shows the use of all 6 math functions.
3. Try below program and check the output :
print(“14 / 3 = “, 14 / 3) |
Modular Programming
Modular programming is a software design technique, which is based on the general principal of modular design. Modular design means that a complex system is broken down into smaller parts or components, i.e. modules. These components can be independently created and tested. In many cases, they can be even used in other systems as well.
There is hardly any product nowadays, which doesn’t heavily rely on modularisation. Computers belong to those products which are modularised to the utmost. So, what’s a must for the hardware is an unavoidable necessity for the software running on the computers.
If you want to develop programs which are readable, reliable and maintainable without too much effort, you have to use some kind of modular software design. Modular programming is a software design technique to split your code into separate parts. These parts are called modules. The focus for this separation should be to have modules with no or just few dependencies upon other modules.
Python Packages
Every file, which has the file extension .py and consists of proper Python code, can be seen or is a module! There is no special syntax required to make such a file a module. A module can contain arbitrary objects, for example files, classes or attributes. All those objects can be accessed after an import. There are different ways to import a modules. Detailed information on creating and executing Packages are mentioned in Functions and Modules chapter.
Python documentation
First of all, Python is very well documented. There might even be copies of these documents on your computer, which came with your Python installation. Please visit official website of Python documentation: https://docs.python.org/3/
Now that we are comfortable with Python programming, lets get deeper into the programming from the next chapter.