Learn Python through C ++ and python
Will C ++ start to "copy" 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
Const char * path = r "C: \ Python27 \ Doc ";
Range Loop
Writing a for Loop in Python is a very comfortable thing:
Copy codeThe Code is as follows:
For x in mylist:
Print (x );
As we all know, I can do the same thing in C ++ 11:
Copy codeThe Code is as follows:
For (int x: mylist)
Std: cout <x;
Automatic type Derivation
Is there a type concept in Python? (Laugh
Copy codeThe Code is as follows:
X = "Hello World"
Print (x)
C ++ 11 also learned this trick, but kept the old lady's foot cloth (auto ).
Copy codeThe 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.
Copy codeThe Code is as follows:
Triple = (5, "Hello", True)
Print (triple [0])
Well, I will use C ++ 11 to show pictures and tigers:
Copy codeThe 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.
Copy codeThe Code is as follows:
X, y, z = triple
Hum, isn't C ++ good?
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe Code is as follows:
Auto mylist = std: vector <int> {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.
Copy codeThe 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:
Copy codeThe Code is as follows:
Auto myDict = std: unordered_map <int, const char * >{{ 5, "foo" },{ 6, "bar "}};
Std: cout <myDict [5];
Lambda expressions
Python provides a big artifact. In 1994, there was a Lambda expression:
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe 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:
Copy codeThe Code is as follows:
Auto result = std: vector <int> {};
Std: copy_if (mylist. begin (), mylist. end (), std: back_inserter (result), [] (int x) {return x> = 0 ;});
Such functions are not uncommon in <algorithm>, and they all echo a function 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.
Copy codeThe 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 ).
Copy codeThe Code is as follows:
Void foo (std: initializer_list <int> 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 ++.