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 displays a full Twitter Url for a user. | |
#Knowledge required here is to join/concatenate a string and request user input | |
#Ask the user their names: | |
f = input ("What is your name?: ") | |
#Create a Twitter string Url and add it to a variable | |
t = "https://twitter.com/" | |
#Ask the user to supply his user name and add the input to a variable | |
h = input('What is your Twitter Handle? (Letters Only): ') |
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
values = [23, 52, 59, 37, 48] | |
sum = 0 | |
length =0 | |
for value in values: | |
sum += value | |
length += 1 | |
print("Total sum: " + str(sum) + " - Average: " + str(sum/length)) |
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
teams = [ 'Man u', 'Tottenham', 'Arsenal', 'Chelsea', 'Liverpool' ] | |
for home_team in teams: | |
for away_team in teams: | |
if home_team != away_team: | |
print(home_team + " vs " + away_team) |
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 multiples(x): | |
for n in range(100): | |
n = (n*x) | |
if n <= 100: | |
print(n) | |
print(multiples(7)) |
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 is_power_of(number, base): | |
# Base case: when number is smaller than base. | |
number = number/base | |
if number < base: | |
# If number is equal to 1, it's a power (base**0). | |
return False | |
else: | |
return True | |
# Recursive case: keep dividing number by base. |
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
#using recursion | |
def sum_positive_numbers(n): | |
if n <= 1: | |
return n | |
return n + sum_positive_numbers(n-1) | |
print(sum_positive_numbers(3)) # Should be 6 | |
print(sum_positive_numbers(5)) # Should be 15 | |
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
#recursions example-2 | |
def factorial(n): | |
print("Factoriaal called with " + str(n)) | |
if n < 2: | |
print("Returning 1") | |
return 1 | |
result = n * factorial(n - 1) | |
print("Returning " + str(result) + " for factorial of " + str(n)) | |
return result | |
factorial(4) |
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
#recursions example-2 | |
def factorial(n): | |
print("Factoriaal called with " + str(n)) | |
if n < 2: | |
print("Returning 1") | |
return 1 | |
result = n * factorial(n - 1) | |
print("Returning " + str(result) + " for factorial of " + str(n)) | |
return result | |
factorial(4) |
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
#7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: | |
#X-DSPAM-Confidence: 0.8475. Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution. | |
#You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are testing below enter mbox-short.txt as the file name. | |
fname = input("Enter file name: ") | |
fh = open(fname) | |
count = 0 | |
average = 0 | |
for line in fh: | |
if not line.startswith("X-DSPAM-Confidence:") : continue |
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 digits(n): | |
count = 0 | |
if n == 0: | |
return 1 | |
while (n > 0): | |
count += 1 | |
n = n // 10 | |
return count | |
print(digits(25)) # Should print 2 |
OlderNewer