Learning Python through C ++

Source: Internet
Author: User
This article mainly introduces how to learn Python through C ++ and through comparative analysis, so that we can better learn python. I will casually say that C ++ has been "plagiarized" Python in recent years? I will only say that I am using C ++ to learn Python.

Believe it? Follow me to learn?

Literal

As early as version 2.6, Python supports taking binary as the literal 1. recently, C ++ 14 has gradually matured and just supported this 2:

The code is as follows:


Static const int primes = 0b101000001000101000101000101100;

Not to mention the concept of raw string literals in Python 1.5. C ++ is not too late, and C ++ 11 has similar practices:

The code is as follows:


Const char * path = r "C: \ Python27 \ Doc ";
Range Loop

Writing a for loop in Python is a very comfortable thing:

The code is as follows:


For x in mylist:
Print (x );

As we all know, I can do the same thing in C ++ 11:

The code is as follows:


For (int x: mylist)
Std: cout <x;

Automatic type derivation

Is there a type concept in Python? (LAUGH

The code is as follows:


X = "Hello World"
Print (x)

C ++ 11 also learned this trick, but kept the old lady's foot cloth (auto ).

The code is as follows:


Auto x = "Hello World ";
Std: cout <x;

Tuples

The tuple in Python has been enviable for a long time. Python has been around since the very beginning.

The code is as follows:


Triple = (5, "Hello", True)
Print (triple [0])

Well, I will use C ++ 11 to show pictures and Tigers:

The code is as follows:


Auto triple = std: make_tuple (5, "hello", true );
Std: cout <std: get <0> (triple );

Some people have said that Python is a good method and can be reverse parsed into variables.

The code is as follows:


X, y, z = triple

Hum, isn't C ++ good?

The code is as follows:


Std: tie (x, y, z) = triple;

Lists

In Python, Lists is the built-in type 4, and creating a list is extremely simple:

The code is as follows:


Mylist = [1, 2, 3, 4]
Mylist. append (5 );

Previously we could say that std: vector can do the same thing. But Python is actually cool. can you initialize it like above? This made the Bjarne Stroustrup father hear this, secretly ashamed, so in C ++ 11, an initializer_list was compiled to respond to 5.

The code is as follows:


Auto mylist = std: vector {1, 2, 3, 4 };
Mylist. push_back (5 );

You can also say that creating a Dictionary in Python is as simple as what it is.

The code is as follows:


MyDict = {5: "foo", 6: "bar "}
Print (myDict [5])

Cut, C ++ itself has the map type, now there is another hash table unordered_map, more like:

The code is as follows:


Auto myDict = std: unordered_map {5, "foo" },{ 6, "bar "}};
Std: cout <myDict [5];

Lambda expressions

Python provides a big artifact. in 1994, there was a Lambda expression:

The code is as follows:


Mylist. sort (key = lambda x: abs (x ))

C ++ 11 has begun a poor imitation:

Std: sort (mylist. begin (), mylist. end (), [] (int x, int y) {return std: abs (x) <std: abs (y );});
Python added a force in 2001 and introduced the Nested Scopes Technology 7:

The code is as follows:


Def adder (amount ):
Return lambda x: x + amount
...
Print (adder (5) (5 ))

C ++ 11 is not a weakness, and the concept of capture-list is 8.

The code is as follows:


Auto adder (int amount ){
Return [=] (int x) {return x + amount ;};
}
...
Std: cout <adder (5) (5 );

Built-in algorithms

Python has many built-in powerful algorithm functions, such as filter:

Result = filter (mylist, lambda x: x> = 0)
C ++ 11 can also use std: copy_if to do the same thing:

The code is as follows:


Auto result = std: vector {};
Std: copy_if (mylist. begin (), mylist. end (), std: back_inserter (result), [] (int x) {return x> = 0 ;});

Such functions are common and echo with some functions in Python: transform, any_of, all_of, min, max.

Variable parameters

Python supports variable parameters from the very beginning. You can define a function with variable parameters. the number and type can be different.

The code is as follows:


Def foo (* args ):
For x in args:
Print (x );
Foo (5, "hello", True)

In C ++ 11, initializer_list supports variable numbers of the same type (C ++ Primer 5th 6.2.6 ).

The code is as follows:


Void foo (std: initializer_list Il ){
For (auto x: il)
Std: cout <x;
}

Foo ({4, 5, 6 });

Can you see that using C ++ to learn Python is also a wonderful way? From the answer to this question, we can see that @ Milo Yip is also the same person.

Continue

Think it's good? Want to make a big shot? Let's take a look at this repo. There are more ways to learn Python using C ++.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.