This document provides an introduction and overview of Python programming including:
- Python is a good first language that is readable and has many open source libraries available.
- It covers Python basics like variables, printing, and file input/output.
- Other constructs covered are functions, branching with if/else statements, lists, tuples, loops, and dictionaries.
- It proposes building a simplified one-player version of the classic Battleship game to practice concepts learned.
3. Why Py?
● A good first language
● Quickly learn to think like a programmer
● Very readable; no arcane syntax
● Plenty of fun projects to work on
● Hundreds of open source libraries to work with
4. Basics
● Hello NITK
● Comments
● Operators and math
● Variables, names, strings, formatting
● Printing
● User input (raw_input, map)
● Import statement
● Command – line arguments using argv
● File reading / writing
5. Functions
● Modularize code
● Similar to argv
● Mini scripts, named code
● Syntax:
def function_name(parameters):
# in function
6. Branching
● If, else and elif
if condition:
# do this
elif condition:
# do this
else:
# do this
7. Lists, Tuples
● Syntax
A = [12, 7, 19]
L = ['kishor', 2, 'string']
● Not restricted to a single type
● Usage (indexing, reading from stdin into a list, etc.)
● Tuples → like lists, but immutable. Use parentheses
instead of square brackets.
8. Loops
● For
my_numbers = [1, 2, 3]
for i in my_numbers:
print i
● While
while condition:
# perform these actions
11. Battleship
● The game is a two player game. It uses grids. On one grid
the player arranges ships and records the shots by the
opponent. On the other grid the player records his/her own
shots.
● Before the game begins, each player secretly arranges their
ships on their primary grid. Each ship occupies a number of
consecutive squares on the grid. The number of squares for
each ship is determined by the type of the ship.
12. Type of ship Size
Aircraft carrier 5
Battleship 4
Submarine 3
Destroyer (or Cruiser) 3
Patrol boat (or destroyer) 2
Each player takes a turn to announce a target square in the opponent's grid which is
to be shot at. The opponent announces whether or not the square is occupied by a
ship, and if it is a "hit" they mark this on their own primary grid. The attacking player
notes the hit or miss on their own "tracking" grid, in order to build up a picture of the
opponent's fleet.
When all of the squares of a ship have been hit, the ship is sunk, and the ship's
owner announces this (e.g. "You sank my battleship!"). If all of a player's ships have
been sunk, the game is over and their opponent wins.
13. • We shall build a simplified, one-player version of the
classic board game Battleship!
• The computer will hide a single ship in a random location
on a 5x5 grid.
• The player will have 10 guesses to try to sink the ship.
• We can try to extend this to implement the actual game
if time permits!!
14. GAME ON
STEP 1:
Set up the board
board=[]
STEP 2:
Let’s initialize the board - initially we shall make all grids ‘O’.
Use a loop and the append() function to initialize.
for i in range(0, 5):
board.append(["O"] * 5 )
Print the board using a loop.
15. •
STEP 3:
We need to hide the ship in a random location.
Let’s use the random function for this.
>> from random import randint
STEP 4:
Let’s get the input from the user (the first guess)
guess_row=int(raw_input("Guess Row: "))
guess_col=int(raw_input("Guess Col: "))
16. STEP 5:
Check if the player’s guess is correct.
Use constructs for this.
STEP 6:
If it’s a wrong guess, then we run a loop to get the player’s
choice again.
If the turns get exhausted then exit the loop.
Tell the player he / she lost.
17. Resources & things to look at
● http://learnpythonthehardway.org/book/
● http://www.pythonchallenge.com/
● Pygame → Make games with graphics and sound
● Pandas → Data manipulation and analysis
● NLTK → analyze written text, write spam filters, chat bots,
etc.
● SimpleCV → computer vision
● ScraPy → get information off web sites
● Ren'Py → interactive fiction
18. Contact Us!
● Kishor → kishorbhat@gmail.com
● Pravalika →avvarupravalika@gmail.com
Of course, you can always contact any IEEE
member via Whatsapp as well.