|
3 | 3 | This repository describes some of the tips and tricks for using the Python programming
|
4 | 4 | language.
|
5 | 5 |
|
6 |
| -## Running Python Scripts |
7 |
| - |
8 |
| -```bash |
9 |
| -python <script name> |
10 |
| -``` |
11 |
| - |
12 |
| -Replace "<script name>" with the file name of the Python script that you want to run. |
13 |
| - |
14 |
| -## Declare Variables |
15 |
| - |
16 |
| -Variables hold the values that are used in your application or script. In Python, |
17 |
| -variables are not strong-typed. This means that the variables you declare, |
18 |
| -are not limited to a particular data type. However, the variable does have a |
19 |
| -value, any future changes to that value must be of the same type. So you cannot set |
20 |
| -the value of a variable to a string and then change it to a integer. |
21 |
| - |
22 |
| -```python |
23 |
| -myVariable = "red" |
24 |
| -myOtherVariable = false |
25 |
| -myOtherOtherVariable = 5 |
26 |
| -``` |
27 |
| - |
28 |
| -## Functions |
29 |
| - |
30 |
| -To write a function in Python, you start the function out with the "def" keyword. |
31 |
| - |
32 |
| -### Example |
33 |
| - |
34 |
| -```python |
35 |
| -def myFunction (param1, param2, param3): |
36 |
| - sum = param1 + param2 + param3 |
37 |
| - return sum |
38 |
| -``` |
39 |
| - |
40 |
| -If you notice, there are no braces or brackets around the contents of the function. |
41 |
| -Python relies on indentation for the grouping of lines together within a given |
42 |
| -section. |
43 |
| - |
44 |
| -## Random Number |
45 |
| - |
46 |
| -Random nubmers can be used to do countdowns or other things that are needed. |
47 |
| -To get a random number, you have to import the randomint class from the random library |
48 |
| -by adding |
49 |
| - |
50 |
| -```python |
51 |
| -from random import randint |
52 |
| -``` |
53 |
| - |
54 |
| -to the top of your Python script. |
55 |
| - |
56 |
| -Then you can set the random integer to a variable by doing |
57 |
| - |
58 |
| -```python |
59 |
| -myNumber = randint(5,60) |
60 |
| -``` |
61 |
| - |
62 |
| -In the example above, 5 is the lower limit of the range and 60 is the upper limit |
63 |
| -of the range. If your range needs to be different, then changes either or both of |
64 |
| -these values. |
65 |
| - |
66 |
| -## Basic Mathematics |
67 |
| - |
68 |
| -### Addition |
69 |
| - |
70 |
| -To do addition in Python, just have to code out two numbers and add them together |
71 |
| -like you would normally do for a math problem that you were writing on paper. Just |
72 |
| -keep in mind that the calculations always have to be done on the right side of the |
73 |
| -equals sign and will be set to the variable on the left of the equals sign. |
74 |
| - |
75 |
| -```python |
76 |
| -sum = 5 + 3 |
77 |
| -print sum |
78 |
| -``` |
79 |
| - |
80 |
| -From the example above, the output will print 8. |
81 |
| - |
82 |
| -### Subtraction |
83 |
| - |
84 |
| -Similar in fashion, subtraction is written just like addition, except it uses the |
85 |
| -minus sign instead of the plus sign. |
86 |
| - |
87 |
| -```python |
88 |
| -difference = 10 - 3 |
89 |
| -print difference |
90 |
| -``` |
91 |
| - |
92 |
| -From the example above, the output will print 7. |
93 |
| - |
94 |
| -### Multiplication |
95 |
| - |
96 |
| -Multiplication is done using the asterisk. |
97 |
| - |
98 |
| -```python |
99 |
| -product = 4 * 3 |
100 |
| -print product |
101 |
| -``` |
102 |
| - |
103 |
| -From the example above, the output will print 12. |
104 |
| - |
105 |
| -### Division |
106 |
| - |
107 |
| -```python |
108 |
| -quotient = 20 / 5 |
109 |
| -print quotient |
110 |
| -``` |
111 |
| - |
112 |
| -From the example above, the output will print 4. |
113 |
| - |
114 |
| -## More Resources |
115 |
| - |
116 |
| -The Raspberry Pi traffic light project is written in Python code. You can review the |
117 |
| -code at |
118 |
| -[https://github.com/almostengr/raspitraffic-stem](https://github.com/almostengr/raspitraffic-stem). |
119 |
| - |
| 6 | +Information listed previous in this repository has been moved to |
| 7 | +[my website](http://thealmostengineer.com/resources/python-cheatsheet). |
| 8 | +You can also view the information |
| 9 | +[on GitHub](https://github.com/almostengr/almostengrwebsite/blob/master/docs/resources/python-cheatsheet.md). |
0 commit comments