10 Basic Python Examples That Will Help You Learn Fast

Posted on

If you’re going to learn a new language today, Python is one of the options out there. Not only is it relatively easy to learn, but it has many practical uses that can come in handy across several different tech-related careers.

This article is for those who already have some programming experience and simply want to transition to Python as quickly as possible. If you have absolutely no programming experience whatsoever, we instead recommend these Python tutorial websites and these online Python courses.

All basic Python examples were written for Python 3.x. We cannot guarantee that they’ll work on Python 2.x, but the concepts should be transferable.computer science computer science

Strings

Proper string manipulation is something that every Python programmer needs to learn. Strings are involved whether you’re doing web development, game development, data analysis, and more. There’s a right way and a wrong way to deal with strings in Python. computer science computer science computer science computer science

String Formatting

Let’s say you have two strings:

>>>name = "Joel"
>>>job = "Programmer"

And let’s say you want to concatenate (“join together”) the two strings into one. Most people might be inclined to do this: computer science  computer science computer science computer science computer science computer science

>>>title = name + " the " + job
>>>title
>"Joel the Programmer"

But this isn’t considered Pythonic. There is a faster way to manipulate strings that results in more readable code. Prefer to use the format() method: computer science computer science computer science computer science

>>>title = "{} the {}".format(name, job)
>>>title
>"Joel the Programmer"

The {} is a placeholder that gets replaced by the parameters of the format() method in sequential order. The first {} gets replaced by the name parameter and the second {} gets replaced by the job parameter. You can have as many {}s and parameters as you want as long as the count matches. computer science computer science computer science

What’s nice is that the parameters don’t have to be strings. They can be anything that can be represented as strings, so you could include an integer if you wish: computer science computer science computer science

>>>age = 28
>>>title = "{} the {} of {} years".format(name, job, age)
>>>title
>"Joel the Programmer of 28 years"

String Joining

Another nifty Pythonic trick is the join() method, which takes a list of strings and combines them into one string. Here’s an example: computer science computer science computer science computer science computer science

>>>availability = ["Monday", "Wednesday", "Friday", "Saturday"]
>>>result = " - ".join(availability)
>>>result
>'Monday - Wednesday - Friday - Saturday'

The defined string is the separator that goes between each list item, and the separator is only inserted between two items (so you won’t have an extraneous one at the end). Using the join method is much faster than doing it by hand.

Conditionals

Programming would be pointless without conditional statements. Fortunately, conditionals in Python are clean and easy to wrap your head around. It almost feels like writing pseudocode. That’s how beautiful Python can be.

Boolean Values

Like in all other programming languages, comparison operators evaluate to a boolean result: either True or False. Here are all the comparison operators in Python: computer science computer science computer science

>>>x = 10
>>>print(x == 10) # True
>>>print(x != 10) # False
>>>print(x <> 10) # False, same as != operator
>>>print(x > 5) # True
>>>print(x < 15) # True
>>>print(x >= 10) # True
>>>print(x <= 10) # True

The is and not Operators

The ==, !=, and <> operators above are used to compare the values of two variables. If you want to check if two variables point to the same exact object, then you’ll need to use the is operator: computer science computer science

>>>a = [1,2,3]
>>>b = [1,2,3]
>>>c = a
>>>print(a == b) # True
>>>print(a is b) # False
>>>print(a is c) # True

You can negate a boolean value by preceding it with the not operator:

>>>a = [1,2,3]
>>>b = [1,2,3]
>>>if a is not b:
>>>    # Do something here
>>>x = False
>>>if not x:
>>>    # Do something here

The in Operator

If you just want to check if a value exists within an iterable object, like a list or a dictionary, then the quickest way is to use the in operator: computer science computer science computer science computer science computer science

>>>availability = ["Monday", "Tuesday", "Friday"]
>>>request = "Saturday"
>>>if request in availability:
>>>    print("I'm available on that day!")
Prev1 of 2Next

Leave a Reply

Your email address will not be published. Required fields are marked *