Yan Cui
I help clients go faster for less using serverless technologies.
Forewords
A while back I decided to try and learn Python for the hell of it as it seems like an interesting language and has some of the most concise and user-friendly syntax. Having spent some time going through a number of different learning sources and materials (like the official site python.org which has a very helpful tutorial section) I have put together a set of notes I made as I was learning and hopefully they can be useful to you as a quick list of how-to code snippets.
All the code snapshots I’m showing here are taken from the IDLE Python shell.
Basics
Comments:
![]()
Variable assignment:
![]()
Arithmetic:
![clip_image003[10] clip_image003[10]](/wp-content/uploads/2011/03/clip_image00310.png)
Power:
![clip_image004[8] clip_image004[8]](/wp-content/uploads/2011/03/clip_image0048.png)
Absolute value:
![]()
Getting user input:
![clip_image006[8] clip_image006[8]](/wp-content/uploads/2011/03/clip_image0068.png)
raw_input vs input:
raw_input always contains string, input can contain any object, even a calculation:
![clip_image007[8] clip_image007[8]](/wp-content/uploads/2011/03/clip_image0078.png)
Import modules:
![clip_image008[8] clip_image008[8]](/wp-content/uploads/2011/03/clip_image0088.png)
Functions as first class objects:
![clip_image009[8] clip_image009[8]](/wp-content/uploads/2011/03/clip_image0098.png)
If-elseif-else:
![clip_image028[8] clip_image028[8]](/wp-content/uploads/2011/03/clip_image0288.png)
The is operator checks if two variables refers to the SAME object:
![clip_image029[8] clip_image029[8]](/wp-content/uploads/2011/03/clip_image0298.png)
on the other hand:
![clip_image030[8] clip_image030[8]](/wp-content/uploads/2011/03/clip_image0308.png)
The is not operator does the reverse.
The and and or logical operators, same as && and || in C# respectively. You can use the not operator to negate the outcome of a boolean comparison.
You can chain comparisons, e.g. is the value of x greater than or equal to 5 and less than or equal to 10?
![clip_image031[8] clip_image031[8]](/wp-content/uploads/2011/03/clip_image0318.png)
You may compare sequence objects of the same type, which uses lexicographical ordering – compare the first two, and if they differ then that’s the outcome of the comparison, else compare the next two, and so on:
![clip_image032[8] clip_image032[8]](/wp-content/uploads/2011/03/clip_image0328.png)
Strings
Strings can use double or single quotes interchangeably:
![clip_image010[8] clip_image010[8]](/wp-content/uploads/2011/03/clip_image0108.png)
Escape character:
![]()
Spanning across multiple lines – a backslash (\) as the last character on the line indicates that the next time is a logical continuation of this line:
![clip_image012[8] clip_image012[8]](/wp-content/uploads/2011/03/clip_image0128.png)
or you can surround them in a pair of matching triple quotes: """ or ”’:
![clip_image013[8] clip_image013[8]](/wp-content/uploads/2011/03/clip_image0138.png)
String conversion using the str() function:
![clip_image014[8] clip_image014[8]](/wp-content/uploads/2011/03/clip_image0148.png)
The repr function – the repr function returns a canonical string representation of the object, back-ticks (`) do the same thing (they are similar to the ToString() method on C#’s objects:
![clip_image015[8] clip_image015[8]](/wp-content/uploads/2011/03/clip_image0158.png)
String concatenation:
![clip_image016[8] clip_image016[8]](/wp-content/uploads/2011/03/clip_image0168.png)
Slicing a string:
![clip_image017[8] clip_image017[8]](/wp-content/uploads/2011/03/clip_image0178.png)
You can also use negative index, in which case it starts counting from the right:
![clip_image018[8] clip_image018[8]](/wp-content/uploads/2011/03/clip_image0188.png)
note: message[0] = message[-0], see how the indices are mapped:
![clip_image019[8] clip_image019[8]](/wp-content/uploads/2011/03/clip_image0198.png)
you can also set up steps in the slicing:
![clip_image020[8] clip_image020[8]](/wp-content/uploads/2011/03/clip_image0208.png)
similarly to before, you can slice backwards too:
![]()
Get length of string:
![]()
Strings are IMMUTABLE!
Formatting strings:
![clip_image023[8] clip_image023[8]](/wp-content/uploads/2011/03/clip_image0238.png)
Finding substring (returns the index of the start of the first match):
![]()
Joining strings:
![clip_image025[8] clip_image025[8]](/wp-content/uploads/2011/03/clip_image0258.png)
Changing the case of strings:
![clip_image026[8] clip_image026[8]](/wp-content/uploads/2011/03/clip_image0268.png)
Replacing portions of a string:
![clip_image027[8] clip_image027[8]](/wp-content/uploads/2011/03/clip_image0278.png)
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
