Text Size
Text Size
Exit Full Screen
Python3 Editor
Show
Source Code
View Source Code in Full Screen
#--------------------------------------------------------- # Print Statements #--------------------------------------------------------- # printing string literals - can use double quotes " or single quotes ' # must use the same quotation at start of string at the end of the string print('Hello world') print(2) # can print integer literals (integer literals use the digit characters only) print(5.423) # float literals use the decimal point print() # prints an empty line (used to separate the output) # can use python as a calculator by putting the operation in a print statement print(3 * 5) # multiplication print(3 + 5) # addition print(3 - 5) # subtraction print(3 / 5) # division (this one will print 0.6) print(3 // 5) # floor division (drops all digits after the decimal, this prints 0) print(34 % 3) # mod - returns the remainder after division # (34/3 = 11 remainder 1. This prints out 1) # mathematical expressions are evaluated using an operator precendence system like PEMDAS # in this case, it will evaluate in this order: # parentheses: (3 + 4) = 7 # division: 7 / 5 = 1.4 # multiplication: 1.4 * 2 = 2.8 # prints out 2.8 print((3 + 4) / 5 * 2) print() # there are also some operators you can use on strings. # the + operator does concatenation: # adds the string on the right to the end of the string on the left print("Hello " + "World") print("Hello" * 5) # prints out "HelloHelloHelloHelloHello" #print("Hello" + 3) # causes a TypeError to occur since it cannot add 2 different types # use the end parameter to set how the print statement ends. # By default, this is the newline character, '\n', which causes the next print statement to appear # on the next line. If you set it to " ", then it will put a space on the end of the print statement # and the next print will be on the same line. print("I am a", end=" ") print("DINOSAUR!") # escape sequences, same as Java # \n = newline # \t = tab # \" = quote (use this if your string is surrounded by double quotes ") # \\ = single backslash print("\tHELLOOOOO") print("I AM REPTAR\nHEAR ME RAWWWWR") print() #------------------------------------------------------------- # VARIABLES & INPUT #------------------------------------------------------------- # when declaring variables need to initialize them with a value. If you don't know what # value it will store, set it to None when you create it. But beware, no operations can be done # on a variable that is a None type. x = None # can also set it to a string with quotes, or any other literal (integer, float, boolean, etc.) x = "Hello" print(type(x)) # you can view its type by using the built-in function type() # take in input by using the built-in input() function. The string inside is the prompt that prints # out before waiting for input. # store it in a variable to use its value later x = input("Enter something: ") print(x) # input() ALWAYS returns a string. So if you ask them enter an integer, it will come back as a string a = input("Enter an integer: ") # if you enter 3 in the above input, the next line will not print out 15, it will print out 33333 print(a * 5) # if your variable name is yellow, it is a reserved keyword and cannot be used as a variable name # if your variable name is blue, it is a built-in function and will affect your program later # if you try to use that same built-in function # use triple quotes """ or ''' at the start and end of a string to make a long string # these can also be used as block comments print('''Hello World This is a long striiiiing.''')
CS110_L1.py
( around 73 lines Python3 code )
Published By:
Mentor Sabrina
Created on
2020-10-16T18:58:13Z
Python3 Editor
- an
OYOclass
application,
own your own class today
.
Run
Result
Status:
Program is not running