This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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]]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |