0% found this document useful (0 votes)
24 views

Name "Dave": End - Quote Find Start - Quote

This document contains code snippets and explanations about Python functions and operations including: 1) Printing a variable name, slicing strings, finding substrings, and converting types. 2) Extracting links from HTML using find(), slicing urls, and handling float/int operations. 3) Defining functions to calculate squares, compare numbers, find largest of 3, and calculate factorials using while loops and returning values.

Uploaded by

Hyeon Soh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Name "Dave": End - Quote Find Start - Quote

This document contains code snippets and explanations about Python functions and operations including: 1) Printing a variable name, slicing strings, finding substrings, and converting types. 2) Extracting links from HTML using find(), slicing urls, and handling float/int operations. 3) Defining functions to calculate squares, compare numbers, find largest of 3, and calculate factorials using while loops and returning values.

Uploaded by

Hyeon Soh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python

name = Dave
Print name [3]
e
print name [-1]
e
S[:] == S[0:] == S[:3]+S[3:]
*NOTE: when s is empty
s[n:] works and gives
s[0] gives an error
Finding a word after another word within a text
end_quote = page.find (' " ', start_quote + 1)

function string
str(89) -> 89

Extracting links
Start_link = page.find(<a href=)

Final Quiz
page =('<div id="top_bin"><div id="top_content" class="width960">'
'<div class="udacity float-left"><a href="http://udacity.com">')
start_link = page.find('<a href=')
middle_link = start_link+9
end_link = page.find('"',middle_link)
url = page[(middle_link):end_link]
float vs int
if any one of the variables in operation is float, result is a float
Function
def square(number):
a = number*number
return a

If statement
def bigger(n1,n2):
if (n1>=n2):
a = n1
else:
a = n2
return a
print bigger(8,3)
*indentation is very important. Also using a return function gives
Def bigger(n1,n2)
If a>b:
Return a
Else:
Return b
Def biggest(n1,n2,n3):
Return bigger(bigger(n1,n2),n3)
Def factorial (n)
Result = 1
While n>= 1:
Result = result * n
N = n-1
Return result
Def get_next_targe(page) My solution
def get_next_target(page):
start_link = page.find('<a href=')
#Insert your code below here
while start_link > (-1):
start_quote = page.find('"', start_link)
end_quote = page.find('"', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote
this is a <a href=http://udacity.com>link

url = "None"
end_quote = 0
return url, end_quote
page = 'not "good" at all'
print url
ANSWER SOLUTION
def get_next_target(page):
start_link = page.find('<a href=')
#Insert your code below here
If start_link == -1:
Return none, 0
start_quote = page.find('"', start_link)
end_quote = page.find('"', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote

def fix_machine(debris, product):


### WRITE YOUR CODE HERE ###
n=0
m=0
a=0
while n ~= len(debris):
while m ~= len(product):
a = debris.find(product[m])
if a == -1:
return "Give me somthing that's not useless next time."
Else:

m = m+1
n = n+1

You might also like