Académique Documents
Professionnel Documents
Culture Documents
Kandarp Gandhi
EE295
009411649
TABLE OF CONTENTS:
PAGE
INTRODUCTION-------------------------------------------------------------------------------------------------------------1
OBJECTIVE--------------------------------------------------------------------------------------------------------------------1
STEPS FOR INSTALLATION------------------------------------------------------------------------------------------------2
EXERCISE 1-------------------------------------------------------------------------------------------------------------------3
EXERCISE 2-------------------------------------------------------------------------------------------------------------------3
EXERCISE 3-------------------------------------------------------------------------------------------------------------------4
EXERCISE 4-------------------------------------------------------------------------------------------------------------------4
EXERCISE 5-------------------------------------------------------------------------------------------------------------------5
EXERCISE 6-------------------------------------------------------------------------------------------------------------------6
EXERCISE 7-------------------------------------------------------------------------------------------------------------------7
EXERCISE 8-------------------------------------------------------------------------------------------------------------------7
EXERCISE 9-------------------------------------------------------------------------------------------------------------------8
EXERCISE 10-----------------------------------------------------------------------------------------------------------------9
EXERCISE 11-----------------------------------------------------------------------------------------------------------------10
EXERCISE 12-----------------------------------------------------------------------------------------------------------------11
EXERCISE 13-----------------------------------------------------------------------------------------------------------------12
EXERCISE 14-----------------------------------------------------------------------------------------------------------------13
EXERCISE 15-----------------------------------------------------------------------------------------------------------------14
EXERCISE 16-----------------------------------------------------------------------------------------------------------------15
EXERCISE 17-----------------------------------------------------------------------------------------------------------------15
EXERCISE 18-----------------------------------------------------------------------------------------------------------------16
EXERCISE 19-----------------------------------------------------------------------------------------------------------------17
EXERCISE 20-----------------------------------------------------------------------------------------------------------------17
EXERCISE 21-----------------------------------------------------------------------------------------------------------------18
EXERCISE 22-----------------------------------------------------------------------------------------------------------------19
EXERCISE 23-----------------------------------------------------------------------------------------------------------------20
EXERCISE 24-----------------------------------------------------------------------------------------------------------------22
REFERENCE------------------------------------------------------------------------------------------------------------------24
1
INTRODUCTION:
Python is a powerful high-level scripting and programming language. It provides simple readability to
users and coders while simultaneously reducing the number of lines one uses to express an idea when
compared to traditional programming languages like C++ or Java. Python finds its application in a wide
range of areas from computing, statistics to various other fields like bio-numeric calculations. Python
enjoys an upper hand over other languages like Perl due to its fairly easy style of coding and mainly
because for non-computer people, learning computer science concepts become fairly simple.
One of the most important features of Python is the vast collection of libraries that are available
for almost every application. These libraries contain in built functions that can be imported into the file
where one writes the Python code. Another important feature of Python is the use of indentation as
syntax. Python uses white space for indentation, indicating that a new block of code has started. The
keywords that Python uses are easy to understand because they are mostly common English words. The
conditional statements like loops and if are very east to implement in Python. Also, implementation of
arrays and lists can be done without much hassle.
There are many useful features of Python excluding the above mentioned ones. These features
not only make Python a powerful language but also provide users with a language that is easy to pick up.
Python can be easy to learn for people who have no prior programming language experience as it has
such good features.
OBJECTIVE
The main objective of this paper is to teach Python to those people who have some or almost zero
programming experience. This paper aims to teach the fundamentals of Python programming so that
those with no experience in programming can learn how to program and update their skills. Also, all
those students who want to enter into the software industry should read this paper to get the basic
knowledge of Python programming.
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27
", "User")
5. In Python, type quit(), and press Enter
6. Learn how to make and change directories in Powershell and also how to open files and close
them.
FOR LINUX:
1. Linux already has the gedit text editor installed in it. Use it for writing the code.
2. In gedit, change the following features:
1. Increase the tab width to 4.
2. Select the checkbox that says Insert spaces instead of tab.
3. Check the Automatic Indentation
4. Also, turn on the display line numbers.
3. Run the XTerm or Terminal in the system.
4. A command line input is needed. Here type python. Python will start
5. Type quit ( ) to exit the application.
6. For using Terminal, learn how to make directory, change a directory and how to access other
files.
NOTE:
Do not install Python 3. Currently, the most popular version to start learning to code is Python 2. If one
begins learning using Python 2, it will be easy to shift on to Python 3, but vice versa will not be easy.
Also, there is no particular operating system that is better to learn Python. If one is comfortable with
Linux, use Linux or else use Windows. Remember, one only needs a text editor, a Terminal or Powershell
and Python to start learning.
Now, after completing typing the following text in the text editor, save the file with the desired name
and a .py extension. For example, example1.py. It is important to save it in this way as .py indicates
that it is a Python code file.
One of the most important commands in Python is the print command. Print command prints all the
data that is between the double quotes everything as it is. Now, to run the example1.py program, open
terminal and type the following in the command line:
>python example1.py (and press enter).
Make sure that the directory is correct while you enter the following command. If not, there will be an
error.
One should be able to see the following as the output:
$ python example1.py
Hello there world ! How is it going
This is the first Python Program
It is going to be fun learning this language
Oh well ! This stuff is being printed
Well, it looks nice, doesnt it?
Now, run the following program in the same way like the previous one and observe the output.
One would notice that whatever was written after the # character does not get printed or displayed
in the output. Thus, comments are used to explain parts of code to others.
EXERCISE 3: USING SOME BASIC MATH
Addition, subtraction, multiplication, etc. are simple mathematical operations that can be
performed very easily in Python. The following program will teach how to use mathematical
operations.
Open a new text file and name it example3.py. Write the following code:
print We will now count the number of balls in the court
print Basket balls, 25 + 30 / 5
print Baseballs, 100 - 25 * 3 % 4
print Now, we will count the total number of bats
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
One should get the following output after running it.
$ python example3.py
We will now count the number of balls in the court
Basket balls 31
Baseballs 97
Now, we will count the total number of bats
7
Answer = True
Question = Are you an engineer? %r
print Question % Answer
The output for this program would be as follows:
$ python example5.py
My name is Bond. James Bond. Code name 007.
Are you an engineer? True
10
goal.write(line c)
goal.write("\n")
print "Closing the new file finally."
goal.close ()
One should be able to see the following output:
$ python ex11.py test.txt
Erase the file named 'test.txt'.
To stop this process, hit CTRL-C (^C).
To continue this process, hit RETURN.
?
Opening the file...
Erasing the file. Swoosh!
Please enter three new sentences.
line a: John is a good man.
line b: The dog is black.
line c: Peter was going to the mall.
Copying these lines to the file.
Closing the new file finally.
11
result.close ()
in_file.close ()
12
16
statement is true. If the condition is false, the if block will be bypassed and the compiler will
execute the remaining commands.
The following program will give more insight into the if statement and its use. Name the below
program as example 20.py and save it.
peter = 20
carl = 30
dan = 15
if peter < carl:
print "Age of carl is more than that of peter."
if peter > carl:
print "Age of peter is more than that of carl."
if peter < dan:
print "Age of dan is more than that of peter."
if peter > dan:
print "Age of peter is more than that of dan!"
dan += 5
if peter >= dan:
print "Peter and dan are greater than or equal in ages."
if peter <= dan:
print "Peter and dan are less than or equal in ages."
if peter == dan:
print "Peter and dan are of equal age."
The output of this program should be as follows:
$ python example20.py
Age of carl is more than that of peter.
Age of peter is more than that of dan.
Peter and dan are greater than or equal in ages.
Peter and dan are less than or equal in ages.
"Peter and dan are of equal age.
18
20
21
23
REFERENCES
1. http://learnpythonthehardway.org/book
24