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:
Power:
Absolute value:
Getting user input:
raw_input vs input:
raw_input always contains string, input can contain any object, even a calculation:
Import modules:
Functions as first class objects:
If-elseif-else:
The is operator checks if two variables refers to the SAME object:
on the other hand:
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?
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:
Strings
Strings can use double or single quotes interchangeably:
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:
or you can surround them in a pair of matching triple quotes: """ or ”’:
String conversion using the str() function:
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:
String concatenation:
Slicing a string:
You can also use negative index, in which case it starts counting from the right:
note: message[0] = message[-0], see how the indices are mapped:
you can also set up steps in the slicing:
similarly to before, you can slice backwards too:
Get length of string:
Strings are IMMUTABLE!
Formatting strings:
Finding substring (returns the index of the start of the first match):
Joining strings:
Changing the case of strings:
Replacing portions of a string:
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.