Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.
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.
Getting user input:
raw_input vs input:
raw_input always contains string, input can contain any object, even a calculation:
Functions as first class objects:
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 can use double or single quotes interchangeably:
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:
Slicing a string:
You can also use negative index, in which case it starts counting from the right:
note: message = 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!
Finding substring (returns the index of the start of the first match):
Changing the case of strings:
Replacing portions of a string:
I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.
Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. Enrol now and enjoy a special preorder price of £9.99 (~$13).
Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong