Text Size
Text Size
Exit Full Screen
Python3 Editor
Show
Source Code
View Source Code in Full Screen
# Virtual Learning Room: 802597 #----------------------------------------------------------------- # Functions #----------------------------------------------------------------- # Define functions # Calling functions # def funcName(param1, param2, ...): # code to be executed # return x def add(a, b, c, d): #total = a + b + c + d return a + b + c + d n1 = 12 n2 = 4.3 n3 = 9 n4 = 2.5 total = add(n1, n2, n3, n4) * 5 print('total =', total) concat = add('S', 't', 'a', 'r') print('concat =', concat) #------------------------------------------------------------------------------------ # Formatting Strings and Print statements #------------------------------------------------------------------------------------ a = 12 b = 5.35792 # METHOD 1: Concatenation # GOAL: print out '12 + 5.35792 = 17.35792' s1 = str(a) + ' + ' + str(b) + ' = ' + str(a + b) print(s1) # METHOD 2: Using commas -- Can ONLY be done in print statements print(a, '+', b, '=', a+b) print(a, end=' ') print('+', end=' ') print(b, end=' ') print('=', end=' ') print(a+b) # METHOD 3: Using % to format the string -- this is deprecated s2 = '%d + %.2f = %.2f' % (a, b, a+b) print(s2) # METHOD 4: Using the format() function on a string s3 = '{} + {} = {}'.format(a, b, a+b) print(s3) print('{:05d} + {:.2f} = {:.2f}'.format(a, b, a+b)) print('\n') #-------------------------------------------------------------------------------------- # Splitting a String #-------------------------------------------------------------------------------------- list_num = "3, 4, 5, 6, 7, 8" list_split = list_num.split(", ") print(list_split) begin = "11:45" begin = begin.split(":") print(begin) print('\n') #-------------------------------------------------------------------------------- # Getting Types and Converting Values #-------------------------------------------------------------------------------- x = input('Enter something: ').lower() print(type(x)) n = input('Enter an integer: ') print('n is', type(n)) n = int(n) print('n is', type(n)) n2 = input('Enter a number: ') print('n2 is', type(n2)) n2 = float(n2) print('n2 is', type(n2)) #-------------------------------------------------------------------------------- # while loops #-------------------------------------------------------------------------------- done = input('Are you done yet? y/n: ').lower() while done != 'y': print('Awww....') done = input('Are you done yet? y/n: ').lower() while True: if done != 'y': print('Awww....') done = input('Are you done yet? y/n: ').lower() else: break
CS110_Review_113020.py
( around 80 lines Python3 code )
Published By:
Mentor Sabrina
Created on
2020-11-30T20:52:03Z
Python3 Editor
- an
OYOclass
application,
own your own class today
.
Run
Result
Status:
Program is not running