### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# Introduction to Programming Python CS-UY 1114

NYU

### View Full Document

## 11

## 0

## Popular in Python

## Popular in Department

This 11 page Bundle was uploaded by Shlomo Oved on Monday August 22, 2016. The Bundle belongs to CS-UY 1114 at New York University taught by Itay Tal in Fall 2015. Since its upload, it has received 11 views.

## Similar to CS-UY 1114 at NYU

## Popular in Subject

## Reviews for Introduction to Programming Python

### What is Karma?

#### Karma is the currency of StudySoup.

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 08/22/16

CS1114 Notes The Sorting Problem- Input: A sequence of numbers <a1, a2, a3…an> Output: A permutation (reordering of the input) <a1’,a2’,…..an’> sequence, such that they are ordered in increasing order. Example:Input: <76,5,34,41,3,7> Output: <3,5,7,34,41,76> Computational Problem: When stating a computational problem we define: 1. A set of legal inputs such as a sequence of numbers 2. Input and Output relationship, i.e. the desired output for every legal input. It’s called the Input/output relationship. Algorithm: It is a solution to a computational problem that is a series of well-defined computational steps that take the input and transform it to its desired output. Note: The computational problem describes what you want to do, and the algorithm describes how you are going to do it. Basic Architecture of a Computer CPU Keyboard Mouse Camera Microphone Screen Microphone Input Sources Output Sources RAM Hard Drive Storage Each transistor is a bit 8 bits= 1 byte for storage. Both RAM and Hard Drive There are no real zeros and ones. If a transistor has a 1 it conducts electricity and it is has a 0 it doesn’t conduct electricity. The zeros and ones stand for physical states Example: Representing 375 as zeros and ones. Base 10 (Decimal)- natural number system of the human beings Digits: 0,1,2,3,4,5,6,7,8,9 0 1 2 3 4 5 6 7 8 9 10- One group of ten and no additional single elements 11 12 13 14 15 16 17 18 19 20- Two groups of ten and no additional single elements . . . 99 100- One group of one hundred and no single/ten elements Base 4 Digits: 0,1,2,3 0 1 2 3 10- one group of 4 and no single elements 11 12 13 20- two groups of 4 and no single elements . . 33 . . 100 375 10this is an obvious number for us 10 10 10 0 320 4320 base 4)- not so obvious as to what number it is 4 4 4 0 2 1 0 3*4 +2*4 +0*4 =56 Base 2 (Binary) Digits: 0,1 0 1 10 11 100 101 110 111 1000 Example: 101001 =241 2 2 2 2 2 2 1 0 Base 16 (Hexadecimal) Digits: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f 0 1 2 3 4 5 6 7 8 9 a-10 single objects b c d e f- 15 single objects 10 11 12 . . . 1f 20 . . . Ff 100 Bf 1611*16 + 15*16 +15*16 0 16 16 16 0 Binary↔Decimal Hexa→Decimal Figure out how to change 75 base 10 to binary 9/9/15 We learned last week how to translate from Binary to decimal How do we change decimal to Binary? 75 10(1001011) 2 Use powers of 2 to figure it out. Start with the highest number less than 75, which is 6 2 or 64 1+2+4+8+16….+2 =2 n n+1-1 Hexadecimal (base 16) ↔ binary Hexadecimal Digits 4 bit binary representation 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A (10) 1010 B (11) 1011 C (12) 1100 D (13) 1101 E (14) 1110 F (15) 1111 3b9 1601110111001 2 It was translated separately by each digit (zeros in the beginning aren’t necessary) 0001011100101100 =(172c)2 16 100101111011 = 2 0 1 2 3 4 5 6 7 8 9 10 11 1*2 +1*2 +0*2 +1*2 +1*2 +1*2 +1*2 +0*2 +1*2 +0*2 +0*2 +1*2 0 1 2 3 0 0 1 2 3 4 (1*2 +1*2 +0*2 +1*2 )2 + (1*2 +1*2 +1*2 +1+0*2 )2 + (1*2 +0*2 +0*2 +1*2 )2 3 8 2 =16 0 4 1 2 =16 2 =16 2 0 1 2 b*16 +7*16 +9*16 =97b 16 Using binary we can represent diferent kinds of data: Numbers Text Images (R,G,B) Each colors value ranges from 0 to 255 Audio Programs Unicode for letters-http://unicode-table.com/ Ram has instructions for the CPU The CPU speed is measured by cycles Program execution process: In each CPU Cycle it completes 4 tasks 1. It fetches the instructions from the RAM 2. It decodes the instructions 3. Execute the instructions 4. Store the result back into memory 10010110 11001101 11110010 10010111 . . Machine Language High-level programming languages (they are languages that aren’t binary) Java Python C C++ Java Script There is a translation process that turns the Python Program into machine language. We either use a: 1. Compiler- translates the whole language at once into machine language 2. Interpreter- translates each instruction as it is executed Important Things to know: 1. Each data object in Python has a type 2. There are a few build in types in Python: int (integer), float (decimal number), complx (complex numbers), str (strings/text), bool (Boolean), list, dict, 3. You can check the type of an object using the type (…) function 4. Diferent types difer in: Representation methods: (int) 7:00000111 (str) “7”:00110111 Operations that can be applied 9/16/15 int()- Used to represent: Integer numbers Method of representation: 2’s complement Ex: 20 : 00010100 -20 : 11101100 (Bolded is sign bit) Literals: (defined values) Ex: 20, -178, 1784065836583463…. 0b10100 (binary representation of 20, 0b is the binary prefix) 0x14 (hexadecimal representation of 20, 0x is the hexadecimal prefix) Operators: +, -, *, / (when you divide two integers,, you get a float), 5/2 =2 R1 5 div 2= 2 (quotient) (//) 5 mod 2=1 (remainder) (%) Power of something (**) Please enter the time of travel in (days, hours, minutes, hours, seconds) format: Number of days: 4 Number of hours: 11 Number of Minutes: 35 Number of seconds: 28 4 days, 11 hours, 35 minutes, and 28 seconds are 387328 To use math symbols, “import math” Then use math.(the math command you want) Ex: math.sin(math.pi) To have python explain a command, use help() To round a number, use command: Round(value, number of digits to round to) 9/21/15 Building Blocks of Programming Languages 1. Data- int, float, str, bool…. 2. Statements and Expressions- Assignments (x=5), input, print, numerical expressions (2+4, 3**4), Boolean expressions 3. Control Flow- Default control flow (sequential flow), selection statements (if, if-else, if-elif-else), Iterative statements (while, for) If Boolean-expression: ----------- ----------- ----------- ----------- When using the print function, use sep=’’ to change default space Turtle: Forward() Right/left() Penup() Pendown() Home() Circle() Pos() (x,y) Set pos(_,_) The len() function Indexing Slicing- When slicing type string [start:finish] String methods String Methods Ex: s= “aBcD” .islower() returns true only when all characters are lowercase letters .isupper() returns true only when all characters are uppercase letters .isdigit() returns true only when all characters are digits .lower() turns the string all to lowercase characters .upper() turns the string all to uppercase characters .find() will give you the location of the character in the string you are trying to find. If two of the same characters it will only give you the index of the first. If it isn’t there it will say -1. .find(‘b’,4) the number tells the method where to start searching for this character Ord() to convert the characters to their ASCII value Chr() to convert the ASCII value to their characters Slicing: ‘string’[start point:endpoint] (not including endpoint) Turtle.color() turns that line that color Function: Len(“abcd”) vs. method: ‘abcd’.islower (diferent syntactically) While (Boolean expression): _____________________ _____________________ _____________________ _____________________ For variable in iterable_collection: _____________________ _____________________ _____________________ _____________________ Break will stop a for loop Iterable_collection: 1.string object 2. range( , ) 10/7/15 Super Even Number= Number that all of its digits are even. Ex: 260 and 2248 Flag- can either be down or raised 10/19/15 Caesar Cipher- A way of encrypting messages Message: ‘example’ Shift by 2 A b c d e f g h I j k l m n o p q r s t u v w x y z Encoded message: gzcorng #Shlomo Oved #Caesar Cipher- A way of encrypting messages #message: 'example' #shift by 2 #encoded messgae:gzcorng num=input('Please enter positive integer') shift_by=int(input('What shift should we apply?')) enc_number='' for dig_char in num: dig=int(dig_char) enc_dig=(dig+shift_by)%10 enc_number+=str(enc_dig) print(enc_number) string=input('Please enter positive integer') shift_by=int(input('What shift should we apply?')) enc_string='' for letter in string: uni=ord(letter) ofset=uni-ord('a') enc_ofset=(ofset+shift_by)%26 enc_uni=enc_ofset+ord('a') enc_letter=chr(enc_uni) enc_string+=enc_letter print(enc_string) Variable Value it points to Input_word ‘cat’ Shift_by 8 Enc_word ‘’ →’k’ →’ki’ →’kib’ Letter ‘c’→’a’ →’t’ Uni 99→97→116 Ofset 2→0→19 Enc_ofset 10→8→1 Enc_uni 107→105→98 Enc_letter ‘k’→’I’ →’b’ 10/21/15 Definition: Let n be an integer >2, we say that n is prime if 1 and n are its only dividers Example: 17 is prime 3571 is prime 6 is not prime Definition: Let n≥2 be an integer And let d be a divider of n. We say that m is d’s complementary divider of n If d*m=n Example: n=100 d=25 m is 25’s complementary divider of 100 (u=4) n=20 d=2 m=10 Lemna: Any integer n>2, has no dividers >n/2, besides n itself Proof: 1. Let d be a divider of n which also d>n/2 We’ll show that d=n 2. Consider m to be d’s complimentary divider of n, m=n/d 3. Since d>n/2, then: n/d<n/(n/2) m<2 m=1 4. Since m=1 and m=n/d, n=d 10/26/15 Defining a function: Def function_name(parameter1, parameter2,…) Body:____________________________ _____________________________ 11/2/15 List: A sequential iterable mutable collection String: a sequential iterable collection of characters Two major diferences between a string and a list: Strings: store only characters Lists: can store objects of diferent types List= [a,b,c,d] Lists are mutable types Strings are immutable types List Literals [[1,2,3,4],[],[1,3.5,’abcd’,[5,7],True] Operator: +, *,==, != Functions: len(list), sum(list), min(list), max(list), Method: append(item)- adds to list insert(index, item)-inserts item pop(index) (deletes item), remove(item)- removes item by value not location count(item)- counts item index(item) list.copy() of list list.reverse() list.sort() Id(lst1)=2120, gives python’s assignment of the list Is tells us it if it points to the same id in the memory There are two major methods to represent data in files: 1. Binary Files- store memory images, that is a copy of the inner representation of the data 2. Text Files- The data is transformed to an equivalent textual representation, which is then saved to the file Text Methods: Text.readline() Text.readlines() For line in file handler: _______ _______ _______ Writing to a file

### BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

#### "When you're taking detailed notes and trying to help everyone else out in the class, it really helps you learn and understand the material...plus I made $280 on my first study guide!"

#### "Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

#### "It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.