CoCalc Public FilesWalton Course Material / Python_practice.ipynb
Author: Katie Agostini
Views : 44
Compute Environment: Ubuntu 18.04 (Stable)
In [1]:
#I am using this file to practice using Python since I am having a difficult time getting familiar with all the rules and commands.

In [8]:
#Create a function that prints out 'Hello World!!'

print('Hello World!!')
print('Hello World!!')
#The print function prints whatever is in the parenthesis and single quotes. You have to have the '' because it is a string and will print what is in the parenthesis and string.

Hello World!! Hello World!!
In [26]:
a='Hello World!'
print(a[0])
print(a[1])
print(a[5])
print(a[:2])
print(a[0:2])
print(a[2:3])
print(a[2:])
print(a[-4:])
print(a[:-3])
print(a[0:-1])
print(a*2)
print(a+a)
print(a + " + " + a)

H e He He l llo World! rld! Hello Wor Hello World Hello World!Hello World! Hello World!Hello World! Hello World! + Hello World!
In [4]:
list=['abc', 123, 'xyz', 4.56]
print (list)
print(list[0])
print(list[1:

 File "<ipython-input-4-7aba8d551493>", line 4 print(list[1:3})]) ^ SyntaxError: invalid syntax 
In [23]:
#Create a function for each of the basic maths operations ie. + - / *
#Once again comment and call in the same "In" block

#I am not sure how if I can perform all of the operations in the same "In" block
3+2
#Is there a way that I can show multiple math operations in the same block becuase whenever I try to do this, it only gives the output for the most recent operation
3-1

2
In [24]:
3*2
#multiplication

6
In [25]:
3**2
#power

9
In [26]:
#division
6/3

2.0
In [36]:
#here I am storing values for variables so that I can use them in an operation.
a=3
b=4
print (a+b)
print (b-a)
#Ok, so I actually just figured out how to show the multiple operations in the same block. The "print" function prints the operation inside of the parenthesis.
#If you want the math operation, you don't use the '' because that prints whatever is inside of them instead of actually doing the math operation.
print ('b/4')
print (b/4)
print (b/a)
c=6
print(c/a)
#Why does dividing always give a decimal in the answer?

7 1 b/4 1.0 1.3333333333333333 2.0
In [55]:
#Create a function that will print out different statements
#For example print normal things using print()
#-using strings ("" or '')
#-using ints, float, tuples

print("Hello")
#The print function above can be written with "" or ''
a='Hello'
print(a)
b=(3, 4, 5)
print (b)
#"b" is a tuple. A tuple is is a collection of objects that are ordered and immutable. Tuples are sequences, just like lists. Lists can be changed, but tuples can not be changed within the parenthesis. Also, tuples use parenthesis () while lists use square brackets []. When you type "print(b)", it will print the tuple that is stored as b.
e='cats'
f='dogs'
c=(2, e, 1, f)
print(c)
d=(2, "cats", 1, 'dogs')
print(d)
#Is there a way to print tuples that have words and numbers in them? And, can you print them without the quotation marks?
g='dog'
print(g)

# The int() function just prints an integer.
int(2)
print("int(49) is:", int(123))
# This prints as a string
print("int('49') is:", int('49'))
int(2)
#I still don't understand the point of the float function, though. It seems to just print the numberwhich int() does already.


Hello Hello (3, 4, 5) (2, 'cats', 1, 'dogs') (2, 'cats', 1, 'dogs') dog int(49) is: 123 int('49') is: 49
2
In [81]:
#Try to make a variable with every often used data types and print values
#Now edit the variables values and print it out again
#feel free to make a function but it is not necessary

#I am going to define a variable "a" as 'Hello!'. I am going to create functions using the variable.
a= 'Hello!'
print(a)
print('a')
print(a.lower())
a.lower()
#I have noticed that if you do not put print() with anything in the parenthesis, then it does not show the result. The print will show everything above it while just typing "a.lower()" does not get stored. Like below
'a'.upper()
a.lower()
print('a'.upper())
print(a.upper())
#I know that once I write another line after "len(a)", the answer for "len(a)" will not be stored. If I put "print" in front of "print(len(a))" then it will store the value for the length of a.
len(a)
print(len(a))

#Below, I have a list "b" and I am showing functions using the list.
b={3, 2, 1, 4}
print(sorted(b))
print(sum(b))
print(type(b))
#Below, I have how you can print and replace letters from a string in a variable.
c='cat'
c.replace('old', 'new')
print(c.replace('c', 'b'))


Hello! a hello! A HELLO! 6 [1, 2, 3, 4] 10 <class 'set'> bat
In [98]:
#Now we are going to use print formatting
#Try to print and format the printing

#I am not sure what you mean here/what is expected.
#Print is used to display whatever is in the parenthesis.
print(2)
print('2')
print('dog')
#If you have words/info that is not defined in the "In" block yet, you have to put the word in quotation marks. Then the print function will show what is within the parenthesis and the quotation marks. You do not really have to do this for numbers because they are usually already defined in the libraries of python.
print(1234567890)
a='Hi my name is '
print(a)
b='Katie'
print(a+b)
#Above, I printed two strings together.

#formatting strings
print('{0} and{1}'.format({20}, {30}))

2 2 dog 1234567890 Hi my name is Hi my name is Katie {20} and{30}
In [107]:
#Make a loop using for loop and a while loop
#Print the results each loop

a={1, 2, 3, 4}
for i in a:
print(i)
#The "for" command goes through each of the inputs ("i") and runs them through whatever it is told to do in the print. You have to remember to put ":" after the for command line.
'\n'
#i did this beause I wanted to separate the numbers that were getting printed between these "for" loops. i do not currently know another way of doing this.
for x in range (3, 6):
print(x)
#"print" always gets parenthesis after it. At the end of a "for" line, there are always ":"
#This for loop above prints value within the range(3, 6) meaning that it includes 3, 4, 5 and not 6 because it goes up to 6.

1 2 3 4 3 4 5
In [125]:
#Make a few if statements with else and elif statements
#Print results

num=3
if num >0:
print(num, 'is positive')

num=-4
if num <0:
print(num, 'is negative')
num=3
if num <0:
print(num, 'is negative')
else:
print(num, 'is positive or zero')


3 is positive -4 is negative 3 is positive or zero
In [135]:
z=-3
if z>0:
print(z, 'is positive')
elif z==0:
print(z, 'is zero')
else:
print(z, 'is negative')
z=0
if z>0:
print(z, 'is positive')
elif z==0:
print(z, 'is zero')
else:
print(z, 'is negative')


-3 is negative 0 is zero
In [145]:
#What is recursion and how is it used and why

#A recursion is a function that calls itself. You define the function using "def ____" and then you use "return" to bring the function back to the start.
n=3
def countdown(n):
if n<=0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
countdown(3)
countdown(4)

3 2 1 Blastoff! 4 3 2 1 Blastoff!
In [146]:
#How would you make a table to display results using what you learned from above


In [155]:
#Tuple vs. List??
#Describe them?

#A tuple is like a list but it cannot be changed or manipulated with. A tuple uses parenthesis () while a list uses {} or []. For example, you can switch the values of certain numbers in a list, like swtiching every [1] in a list with a [3]. However, you cannot do this with a tuple.
tuple=(1, 2, 3)
print(tuple)
#You cannot do: "tuple[1]=3" because this means changing the 1 to a 3 and you cannot do that with tuples because they are immmutable.
list=[1, 2, 3]
print(list)

(1, 2, 3) [1, 2, 3]
In [156]:
#Create a graph with a predefined set of data
#Create a new graph using a function or equation to create data


In [26]:
#Fibonacci Sequence
#The fibonacci sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.
#The first term of the sequence is 0. The second term is 1. The third term is 1 which we get from 0+1. The fourth term is 2 which we get from 1+1. The fifth term is 3 which we get from 1+2. The sixth term is 5 which we get from 2+3.

def F(n):
if n<0:
return("Invalid Input")
elif n==0:
return (0)
elif n==1:
return(1)
elif n==2:
return(1)
else:
Result=(F(n-1)+F(n-2))
print (n, ";", Result)
return Result
print(F(5))

3 ; 2 4 ; 3 3 ; 2 5 ; 5 5
In [27]:
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1


How many terms?
Fibonacci sequence: 0 1 1 2 3 5 8 13
In [28]:
nterms = int(input("How many terms? "))

How many terms?
In [ ]:


In [ ]: