Skip to content

Instantly share code, notes, and snippets.

View pdbartsch's full-sized avatar

Paul Bartsch pdbartsch

  • UC Santa Barbara
  • 93455
View GitHub Profile
# find the minimum value across a list of fields:
# expression:
min([!field1!, !field2!, !field3!])
# find the maximum value across a list of fields:
# expression:
max([!field1!, !field2!, !field3!])
# find the sum of all values from a list of fields:
# expression:
'''
Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a
new list of only the first and last elements of the given list.
'''
import random
a = random.sample(range(1, 100), 15)
def listends(a):
print('',a,'\n',[a for a in [a[0], a[-1]]])
invar = int(input("Please enter an integer to find out whether it's prime or not: "))
def prime(n):
divisors = [ x for x in range(1,n+1) if n % x == 0]
if len(divisors) == 2:
print('prime')
else:
print('not prime')
prime(invar)
import random
a = random.sample(range(1, 100), 15)
b = random.sample(range(1, 100), 25)
c = list(set(a).intersection(b))
print('a:', a, '\nb:', b, '\noverlap:',sorted(c))
import random
num = random.randrange(0,100,1)
score = 0
while True:
guess = input("Guess the number between 1 and 100? -- Type exit to end. ")
if guess.lower() == 'exit':
break
else:
gnum = int(guess)
def rps():
print('ROCK - PAPER - SCISSORS: \nRock beats scissors \nScissors beats paper \nPaper beats rock\n')
while True:
yn = input("Want to play? Yes or No: ").lower()
if yn[0] == 'y':
play()
else:
print('\nOK. Maybe later.')
break
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
even = [n for n in a if n%2 == 0]
odd = [n for n in a if n%2 != 0]
print('even:', even, '\nodd:', odd)
def stringlists():
s = input("please enter a string a.k.a. some text ")
# extended slice syntax with negative 1 step reverses the string ([begin:end:step])
if s[::-1].lower() == s.lower():
return 'Cool! That string was a palindrome.'
else:
return "That string wasn't a palindrome."
import requests
from bs4 import BeautifulSoup
url = 'https://www.nytimes.com/'
result = requests.get(url)
c = result.content
soup = BeautifulSoup(c)
heading = soup.find_all('h2')
for story in heading:
print(story.text.strip())
# https://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html
import random
a = random.sample(range(1, 100), 10)
b = random.sample(range(1, 100), 10)
c = list(set(a).intersection(b))
print('a:', a, '\nb:', b, '\noverlap:',c)