baabtra.com little coder chapter - 2
Post on 15-Jul-2015
353 Views
Preview:
TRANSCRIPT
What you’ve learned?
What is instructions?
How to give instructions sequentially to accomplish a task !
What is Computer programming ?
What are computer languages?
Python
– Python is a computer language which is very easy
to learn and can be used to give instructions to a
computer
1. Goto http://www.python.org/ and download the latest
Windows installer
2. for Python 3. Look for a section in the menu titled Quick
Links,
3. The exact version of Python that you download is not
important, as long as it starts with the number 3.
4. After you download the Windows installer, double-click its icon,
and then follow the instructions to install Python in the default
location, as follows:
i. Select Install for All Users, and then click Next.
ii. Leave the default directory unchanged, but note the name of the
installation directory (probably C:\Python31 or C:\Python32).
Click Next.
iii. Ignore the Customize Python section of the installation, and
click Next.
1. Go to http://www.python.org/getit/ and download the latest installer for
MAC
i. If you’re running a Mac OS X version between 10.3 and 10.6, download the 32-
bit version of Python 3 for i386/PPC.
ii. If you’re running Mac OS X version 10.6 or higher, download the 64-bit/32-bit
version of Python 3 for x86-64.
2. Once the file has downloaded (it will have the filename extension.dmg),
double-click it. You’ll see the file as shown below
3. In this window, double-click Python.mpkg, and then follow the
instructions to install the software
1. Python comes preinstalled on the Ubuntu Linux distribution, but it may
be an older version. Follow these steps to install Python 3 on Ubuntu 12.x:
2. Click the button for the Ubuntu Software Center in the Sidebar(it’s the
icon that looks like an orange bag—if you don’t see it, you can always
click the Dash Home icon and enter Software in the dialog).
3. Enter Python in the search box in the top-right corner of the Software
Center.
4. In the list of software presented, select the latest version of IDLE, which is
IDLE (using Python 3.2) in this example:
5. Click Install.
6. Enter your administrator password to install the software, and then click
Authenticate.
Let’s do programming in python
• Opening Python console !
– Once you install python, You should have an icon on your Windows or Mac OS X
desktop labeled IDLE. If you’re using Ubuntu, in the Applications menu, you
should see a new group named Programming with the application IDLE Double-
click the icon or choose the menu option, and you should see this window:
Let’s do programming in python
• This is the play ground where we give instructions to the
computer to accomplish our tasks. Sooner you will learn about
what all instructions we can give
• In this chapter you we will learn 3 instructions
or commands
– To print a message to computer screen
– To do arithmetic calculations
– To store values into computer memory
Print() Command
• Type below in your console and press enter
>>> print("Hello baabtra, You are awesome")
>>> print("Hello baabtra, You are awesome")
Hello baabtra, You are awesome
>>>
Print() Command
• Type below in your console and press enter
>>> print("Hello baabtra, You are awesome")
>>> print("Hello baabtra, You are awesome")
Hello baabtra, You are awesome
>>>
Congratulations! You’ve just created your first Python program.The word print is a type of Python command called a function, and it prints out whatever is inside the parentheses to the screen. In essence, you have given the computer an instruction to display the words “Hello baabtra, You are aswesome”
Saving your file
1. To save a new program, open IDLE and choose File => New
Window.
2. An empty window will appear, with *Untitled* in the menu bar.
3. Enter the following code into the new shell window:
print("Hello World")
4. Now, choose File=>Save. When prompted for a filename, enter
hello.py, and save the file to your desktop.
5. Then choose Run=>RunModule.
Python Operators
>>>8 * 3.57
Type this an press enter in your python console
>>> 17 + 365
>>>8 * 3.57
28.56
>>> 17 + 365
382
>>> 174 - 36>>> 174 - 36
138
>>> 324 / 36>>> 324 / 36
9
Try This !
• Write python command to find the result of
– 1340+ 1242- 43*4
–(123-12)*12+14
–12*(16/2)+13*2
Variables
• You already know that our computers are having memory to store
things !We can make use of this memory in our program to store
values using something called variables
• The word variable in programming describes a place to store
information such as numbers, text, lists of numbers and text, and
so on.
Creating a variable
• In order to create a variable named ‘studentid’ with value 100,
Type as below and press enter
>>> studentid=100
Creating a variable
• In order to create a variable named ‘studentid’ with value 100,
Type as below and press enter
>>> studentid=100
When you entered studentid=100, the computer
reserved one of the memory for us and assigned a name
‘studentid’ and stored the value 100 inside it.
How to print the value of variable
• To print the value of a variable you can simply give it inside
print() function
>>>print(studentid)
100
>>>studentName=“John”
>>>print(studentName)
John
Printing variable value inside a message
• Suppose if you want to a message as below
“Hello friends, My name is John and my Id is 100”
• It can be done as of below
>>print(“Hello friends,My name is %s and my Id is %d”% (studentName,studentId))
Printing variable value inside a message
• Suppose if you want to a message as below
“Hello friends, My name is John and my Id is 100”
• It can be done as of below
>>print(“Hello friends,My name is %s and my Id is %d”% (studentName,studentId))
Here %s will be substituted with the value of variable
studentName
Printing variable value inside a message
• Suppose if you want to a message as below
“Hello friends, My name is John and my Id is 100”
• It can be done as of below
>>print(“Hello friends,My name is %s and my Id is %d”% (studentName,studentId))
Here %d will be substituted with the value of variable studentid
What is this %s and %d ?
• %s is used as a placeholder for string values you want inject into a
formatted string.
• %d is used as a placeholder for numeric or decimal values.
Type this and press enter in your python console
Now print the below message using print() function
“ Yahoo, I’ve got 45 marks out of 50 in Maths
subject !”
Try this
>>>subjectName=“Maths”
>>>totalMark=50
>>>myMark=45
Type this and press enter in your python console
Now print the below message using print() function
“ Yahoo, I’ve got 45 marks out of 50 in Maths
subject !”
Try this
>>>subjectName=“Maths”
>>>totalMark=50
>>>myMark=45 Answer
>>>print(“ Yahoo, I’ve got %d marks out of %d in %s subject !
%myMark,totalMark,subjectName)
Type this and press enter in your python console
Now calculate your percentage of mark and print the below message
“Hey, I’ve got 90 percentage of mark in Maths”
Try this
>>>subjectName=“Maths”
>>>totalMark=50
>>>myMark=45
Type this and press enter in your python console
Now calculate your percentage of mark and print the below message
“Hey, I’ve got 90 percentage of mark in Maths”
Try this
>>>subjectName=“Maths”
>>>totalMark=50
>>>myMark=45
Answer>>>percentage=myMark/totalMark
>>>print( “Hey, I’ve got %d percentage of mark in %s”%percentage,subject”)
Exercise• John has 19 apples, 21 oranges and 14 bananas to sell
• Write a python program to
• Print the total number of fruits John has?
• If he sell 12 apples, 9 oranges and 14 bananas, print how
many fruit will be left with him?
apple=19
orange=21
banana=14
totalFruits=apple+orange+banana
print(“Total Fruits = %d”% totalFruits)
apple=apple-12
orange=orange-9
banana=banana-14
totalFruits=apple+orange+banana
print(“Total Fruits after selling = %d”% totalFruits)
top related