Top Banner
L i t t l e C o d e r P r o g r a m by
43
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Baabtra.com little coder   chapter - 2

Little Coder Program

by

Page 2: Baabtra.com little coder   chapter - 2

What you’ve learned?

What is instructions?

How to give instructions sequentially to accomplish a task !

What is Computer programming ?

What are computer languages?

Page 3: Baabtra.com little coder   chapter - 2

Start with PythonChapter 2

Page 4: Baabtra.com little coder   chapter - 2

Python

– Python is a computer language which is very easy

to learn and can be used to give instructions to a

computer

Page 5: Baabtra.com little coder   chapter - 2

Setting up python environment in Windows

Page 6: Baabtra.com little coder   chapter - 2

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.

Page 7: Baabtra.com little coder   chapter - 2

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.

Page 8: Baabtra.com little coder   chapter - 2

• At the end of this process, you should have a Python 3 entry in

your Start menu:

Page 9: Baabtra.com little coder   chapter - 2

Setting up the environment in MAC

Page 10: Baabtra.com little coder   chapter - 2

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

Page 11: Baabtra.com little coder   chapter - 2

Setting up the environment in Ubuntu

Page 12: Baabtra.com little coder   chapter - 2

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:

Page 13: Baabtra.com little coder   chapter - 2

5. Click Install.

6. Enter your administrator password to install the software, and then click

Authenticate.

Page 14: Baabtra.com little coder   chapter - 2

Let’s do programming in python

Page 15: Baabtra.com little coder   chapter - 2

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:

Page 16: Baabtra.com little coder   chapter - 2

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

Page 17: Baabtra.com little coder   chapter - 2

Python Commands

Page 18: Baabtra.com little coder   chapter - 2

• 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

Page 19: Baabtra.com little coder   chapter - 2

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

>>>

Page 20: Baabtra.com little coder   chapter - 2

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”

Page 21: Baabtra.com little coder   chapter - 2

Try This !

• Write python command to

–Print your name

–Print Your Age

–Print your hobbies

Page 22: Baabtra.com little coder   chapter - 2

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.

Page 23: Baabtra.com little coder   chapter - 2

Python Operators

Page 24: Baabtra.com little coder   chapter - 2

Python Operators

+ Addition

- Subtraction

* Multiplication

/ Division

Page 25: Baabtra.com little coder   chapter - 2

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

Page 26: Baabtra.com little coder   chapter - 2

Try This !

• Write python command to find the result of

– 1340+ 1242- 43*4

–(123-12)*12+14

–12*(16/2)+13*2

Page 27: Baabtra.com little coder   chapter - 2

Variables

Page 28: Baabtra.com little coder   chapter - 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.

Page 29: Baabtra.com little coder   chapter - 2

Creating a variable

• In order to create a variable named ‘studentid’ with value 100,

Type as below and press enter

>>> studentid=100

Page 30: Baabtra.com little coder   chapter - 2

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.

Page 31: Baabtra.com little coder   chapter - 2

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

Page 32: Baabtra.com little coder   chapter - 2

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))

Page 33: Baabtra.com little coder   chapter - 2

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

Page 34: Baabtra.com little coder   chapter - 2

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

Page 35: Baabtra.com little coder   chapter - 2

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.

Page 36: Baabtra.com little coder   chapter - 2

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

Page 37: Baabtra.com little coder   chapter - 2

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)

Page 38: Baabtra.com little coder   chapter - 2

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

Page 39: Baabtra.com little coder   chapter - 2

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”)

Page 40: Baabtra.com little coder   chapter - 2

Exercise !

Page 41: Baabtra.com little coder   chapter - 2

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?

Page 42: Baabtra.com little coder   chapter - 2

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)

Page 43: Baabtra.com little coder   chapter - 2

End of Chapter 2