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:
Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.
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