Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

10 Basic Python Examples That Will Help You Learn Fast

$
0
0

If you’re going to learn a new language today, python is one of the options out there 5 Reasons Why Python Programming Is Not Useless 5 Reasons Why Python Programming Is Not Useless Python -- You either love it or you hate it. You might even swing from one end to the other like a pendulum. Regardless, Python is a language that's hard to be ambivalent about. Read More . 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 6 of the Best Paying Tech Careers for 2016 6 of the Best Paying Tech Careers for 2016 Investing in the right technology career requires planning and foresight. These six career options are expected to be the hottest in the next few years. Are you ready? Read More .

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 The 5 Best Websites To Learn Python Programming The 5 Best Websites To Learn Python Programming Over the past decade, the Python programming language has exploded in popularity amongst programmers in all areas of coding. From web developers to video game designers to in-house tool creators, many people have fallen in... Read More and these online Python courses 5 Courses That'll Take You from Python Beginner to Pro 5 Courses That'll Take You from Python Beginner to Pro These five courses will teach you all about programming in Python, one of the hottest languages out there right now. Read More .

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.

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.

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:

>>>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:

>>>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.

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:

>>>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:

>>>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:

>>>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 isand 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:

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

Viewing all articles
Browse latest Browse all 9596

Trending Articles