Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:

Similar presentations


Presentation on theme: "Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:"— Presentation transcript:

1 Mr. Crone

2  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints: #Hello1 #Hello2

3  Use a comma to print strings and variables Example) var = 5 print(“Var 1 = “, var)

4  Single line comments begin with: # Example) # This is a comment  Multi-line comments begin and end with: ‘’’ Example) ‘’’ Comment line 1 comment line 2’’’

5  Same as Java: *, +, -, %  Use parentheses for grouping  / always returns a floating point number  // - floor division – returns integer result of division statement Example) print(5/2) # Prints 2.5 print(5//2) # Prints 2

6  Use, **, to calculate powers Example) print(4**2) #Prints 4 squared, or 16

7  Can be declared using single or double quotes Example) x = ‘I am a String’ y = “I am a Sting” # x and y are both Strings

8  \n and \t represent newline and tab respectively  \ is the escape character and can be used to “escape” quotes

9  Use ‘’’ or “”” to create multiline string literals Example) print('''line1 line2 line3 line4''') #Prints: line1 line2 line3 line4

10  Use backslash to prevent automatic newlines Example) print('''line1\ line2\ line3\ line4''') #Prints: line1line2line3line4

11  Strings can be concatenated with: + Example) x = “yes” y = “no” z = x + y #yesno  Strings can be repeated with: * z = x * 3 #yesyesyes

12  Two or more string literals next to each other are automatically concatenated Example) print(‘house’ ‘mouse’) #housemouse

13  Individual characters of Strings can accessed by specifying a character index Example) x = 'hello' print(x[0]) #Prints h  Negative indices start at the end of the string x = 'cat' print(x[-1]) #Prints t

14  Substrings can be accessed with the syntax below Example) x = "Splended" print(x[1:4]) # Prints: ple #1 is included, 4 is not included

15  Strings are immutable – cannot be changed x = “Dog” x[0] = “F” # ERROR

16  Use the length function to return the length of a string len(x) Example) x = “Cat” print(len(x)) #Prints: 3

17  Lists are used to group several values together  Use square brackets to designate a list x = [1, 3, 5, 7] Example) x =[1, 3, 5, 7, "String"] print(x[4]) # Prints: String

18  Use the length function to print the size of a list  x =[1, 3, 5, 7, "String"]  print(len(x)) # Prints: 5

19  Negative indices are allowed x =[1, 3, 5, 7, "String"] print(x[-2]) #Prints: 7  Slicing uses the same syntax as Strings x =[1, 3, 5, 7, "String"] print(x[0:2]) #Prints: [1,3]

20  Lists can be combined with: + Example) x = [1, 3, 5] y = [2, 4] z = x + y print(z) #[1, 3, 5, 2, 4]

21  Lists are mutable and can be modified  Lists can grow in size with the append method Example) x = [1, 3, 5] x.append(7); print(x) # Prints: [1, 3, 5, 7]

22  Same as C++ and Java  >  <  >=  <=  ==  !=

23 Syntax: while condition : body statements Example) x = 1 while x < 5: print(x) x+=1

24 Syntax if condition : body statements elif condition : body statements else: body statements

25  Syntax var = input(“Prompt”) Example) x = input(“Enter a number: “)

26  Variables can be cast to specific types Example) x = float(input(“Enter a number”)) #Ensures that the variable above can be # used in arithmetic expressions int(5.5) #returns 5

27  Use random.random() to generate random numbers in the range [0,1)  Must include, import random, at top of module Example) x = int(random.random() * 10) #Generates random number 0-9

28  Max and Min are built into Python and can be used without an import statement Example) print(max(3, 4)) #Prints 4


Download ppt "Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:"

Similar presentations


Ads by Google