!
25 January, 2013
Class notes
Circles
Example 1. Circle without a permeter
#draws a circle
#parameter
#r: radius of the circle
#n: number of repetitions
#x: forward
#A: angle
def myCircle ():
for i in range (360):
forward (1) #1 is the parameter
left (1)
#Rula AlBaghdadi
#Principles of Computing
#Tuesday, 11th September 2012
#10:30am11:am
from turtle import *
def spiral ():
for i in range (1,13): #This is the defined range, because while you seek 12
repetitions, for i in range (12) counts from 011 (12
040912
Class Notes by Fatima Amir
Principles of Computing 15110
Introduction to Python
A turtle is the little arrow like thing in Python that we are able to command
through different codes, as shown below.
Coding with Python
o Always start with from
September 16th 2012
Class notes
Variables and Values
We can imagine the Variable as a large box and the values is the things we put in that box.
Variables must be called by a letter.
The symbol equal is called assignment operator
3
X
Think about this proc
110912
Class Notes by Fatima Amir
Principles of Computing 15110
Problem #1: Making a concentric square
o Here are the Python commands:
o
Heres the outcome:
110912
Class Notes by Fatima Amir
Principles of Computing 15110
Problem #2: Making a spiral
General Advices of improving your grades:
1. Go to the ARC with ALL your previous quizzes and exercises to discuss it with the TA or peer
tutor.
2. Try connecting the activities we do in class with the ones in the quizm because most of the time
they have
Variables, values, and types
Examples of variables we have already seen are:
The parameters of a function are variables
The "i" in the loop of a defined function
A variable is like a labeled box; the contents of the box (values) are stored and
retrieved
160912
Class Notes by Fatima Amir
Principles of Computing 15110
Variables and values
In the past, the variables we have worked with are:
i, j, k etc for the range loop
As parameters\
Variables can be understood with a simple analogy.
A variable is l
#variables and values
x=3
y=2
z=x+y
the = sign is called the assignment operator, it assigns a value to a variable
print"x is", x
takes whatever is written after the print and pastes it
, means followed by
It shows "x is 3"
print"x is", x
print"x is", y
p
Creating a Spiral
o
We already know how to create lines of set sizes by using the forward()
and right/left() functions
o
In order to create lines of varying sizes, the variable that we use in a for
loop comes in handy
o
If we were to guess how to do this
Principles of Computing
9/16/2012
Concept of Variables, Values & Types
Variables:
We have seen it before in TURTLE>
parameter input
the i in for i in range
Its like a box where we put the value inside
Variables have labels
3
X=3
X

taking a 3 and pu
Manar Naboulsi
9 September, 2012
I.
Nested Loops:
A. List repeated items:
Lines 4 lines make a small square
Squares 3 squares make a row
Rows 3 rows make a large square
B. Start from insideout
Single small square:
We want to repeat this square 3 time
090912
Class Notes by Fatima Amir
Principles of Computing 15110
Nested Loops
o Problem: How can we make a grid like the one below?
Lets break this problem down into smaller pieces. To make a grid, you need to initially make a
square. Heres how you wil
Tues. Sept 11
from turtle import *
def spiral():
for i in range(1, 13):
#use (1, 13) and not(12) to start the counting from 1 and not from zero
fd(10*i) #shortcut of forward
lt(90) #shortcut of left
def spiral2(NumSpikes):
for i in range (NumSpikes):
fd(1
#Values and Types:
# in python, values are not only numbers. There are 6 types:
# Integers > 1, 2, any whole number, as well as zero.
# Floats > 1.5, 2.3333, any number that has a decimal. 0.0 is also
considered
# a float.
# Strings > any type of text,
# String indexing and slicing
#string is a sequence of character
#Example:
#hello
#0123
x = "how are you?"
#start from 0. total of character 12.
print x
#Indexing:
#Picking one character from string.
print x[2]
print x[6]
print x[0]
print x[2]
#negati
15110 Principles of Computing
Sana Britto
Notes for January 24, 2013
Creating a Circle in Different Ways:
The manual and typical way of creating a circle is as follows:
def myCircle(r): # r: radius of the circle
for i in range (360):
forward(1)
left(1)
B
#Adds one to the input
# Parameters:
#
x: a number
# Returns:
#
a number (x incremented by 1)
def addOne(x):
y = x+1
return y
print addOne(5)
print addOne(5)*2
#Print addOne("hey") #error > string and integers can't be added.
print 2*addOne(addOne(5)
#Ad
15110 Principles of Computing
Sana Britto
Notes for January 20, 2013
Code for the Flower made by 12 equal squares:
from turtle import *
for j in range (12): # This repeats the small boxes 12 times
for i in range(4): #Normal code to create a square
forwar
15110 Principles of Computing
Sana Britto
Notes for January 17, 2013
Defining a Function
Sometimes when you need to use a figure repeatedly, you may want to define it in the form of
a function so that it can be easily used as and when required.
For eg: I
15110 Principles of Computing
Sana Britto
Notes for January 29, 2013
ORDER MATTERS: Whenever we write a code, the order in which the code is written have an impact
on the outcome of the code. For example, here are 3 cases in which the outcomes of the cod
#Rula AlBaghdadi
#Principles of Computing
#Sunday, September 9th 2012
# 10:30am  11:50am
from turtle import *
#multiple positions and nested loops
#the 3x3 grid has multiple repeating units: square and row
def grid3x3 ():
for k in range (3): #this overa
060912
Class Notes by Fatima Amir
Principles of Computing 15110
Making a definition with Python
Codes can also be defined with one word so that if one has to repeat that specific code, the
defined word can be used instead. This makes the job simpler a
Logic
1) Recall that a variable can store different kinds of data. A variable can hold a Boolean (logical) value
of True or False. The following Python function prints out what is legal to do for a person in the U.S.
given the person's age, gender, and ci
Random Generators
1) This question deals with generating random numbers using a linear congruential generator (LCG).
Recall the conditions that must hold for an LCG with parameters a, c, and m to have its maximum
period of m:
c and m must be relatively pr
Python Introduction
1) For each of the following Python expressions, show how they are evaluated by evaluating one
operation at a time and showing the reduced expression until you have a final answer. For example, if
the Python expression is 3 * 5 + 5, yo
Programming Technique
Programmers write helper functions as a way of focusing on a single aspect of a complex problem,
allowing them to design and test the code for each piece separately.
Many Python programmers have adopted a process from software engi
Recursion
def recursiveFunction():
if (this is the base case):
# no recursion allowed here!
do something nonrecursive
else:
# this is the recursive case!
do something recursive
def interleave(list1, list2):
# assume list1 and list2 are samelength lists
15110 Spring 2011 Midterm 1
80 minutes
1.
[10 pts; 1 pt each] True or False (circle one):
a. TRUE or FALSE: Any binary number that ends in a 1 must be odd.
b. TRUE or FALSE: Any binary number that contains exactly one 1 must be a power of 2.
c. TRUE or F
1.
[10 pts; 1 pt each] True or False (circle one):
a. TRUE or FALSE: For good style, every line that extends beyond 80 characters should include a clear
comment explaining why this was necessary.
b. TRUE or FALSE: Software errors cost the US Economy about
From: "Saved by Internet Explorer 11"
Subject: Carnegie Mellon University Mail
Date: Fri, 18 Sep 2015 13:10:49 0400
MIMEVersion: 1.0
ContentType: multipart/related;
type="multipart/alternative";
boundary="=_NextPart_000_0000_01D0F213.701B6B90"
XMimeO
Brinda Malhotra
bmalhotr
Lab Section J
Assignment #1
9/11/15
1. Jacquard's loom is considered to be a milestone in the history of computing because it
used punched cards to input the patterns to be weaved by the loom. This resulted in very
complex weaves