Raspberry Pi 3 – Day 4
Python
-
Running Python and Output
-
Data Types
-
Input and File I/O
-
Control Flow
-
Functions
$ python Python 2.4.3 (#1, Nov 11 2010, 13:34:43) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
>>> print "Hello, Python!"
$ python test.py
#!/usr/bin/python print "Hello, Python!"
$ chmod +x test.py # This is to make file executable $./test.py
a = 7 s = 'xyz' l = ['a simpler list', 99, 10]
tuple1 = (1,2,3) # tuples are immutable list1 = [1,2,3] # lists are mutable
var = 100 if ( var == 100 ) : print "Value of expression is 100" print "Good bye!"
def printme( str ): "This prints a passed string into this function" print str return primes = [2, 3, 5, 7] for prime in primes: print(prime) # Prints out the numbers 0,1,2,3,4 for x in range(5): print(x) # Prints out 3,4,5 for x in range(3, 6): print(x) # Prints out 3,5,7 for x in range(3, 8, 2): print(x) # Prints out 0,1,2,3,4 count = 0 while count < 5: print(count) count += 1 # This is the same as count = count + 1
people = 30 cars = 40 trucks = 15 if cars > people: print "We should take the cars." elif cars < people: print "We should not take the cars." else: print "We can't decide." if trucks > cars: print "That's too many trucks." elif trucks < cars: print "Maybe we could take the trucks." else: print "We still can't decide." if people > trucks: print "Alright, let's just take the trucks." else: print "Fine, let's stay home then."
Project
- Running LED ( test.py)