• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering.
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Coming soon.
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models.

MFE: To do or not to do?

Daniel Duffy

C++ author, trainer
I wouldn't use the word "flexible".

I would. But that's allowed.

BTW the Java/C++ war died some time ago.

Programmers can be very clannish at times.

Who cares, really? :D
 

rajanS

Active Member
I knew Java first. Then C++. Then Python. But I feel after programming in Python for 2 years that it's a great first language to learn and prefer to have learned this first than going to the JAVA route.
 

tfors

Active Member
I knew Java first. Then C++. Then Python. But I feel after programming in Python for 2 years that it's a great first language to learn and prefer to have learned this first than going to the JAVA route.
I agree. First learn to think like a programmer, then learn the intricacies of memory management etc.
 

ExSan

Active Member
I knew Java first. Then C++. Then Python. But I feel after programming in Python for 2 years that it's a great first language to learn and prefer to have learned this first than going to the JAVA route.
so please help me with this:
I need the equivalent structure in python of these C structures:

do{
// my code
}while(exp?);

I want to translate some C code to python, also I need the equivalent python code for this

switch(?){
case i: //code ; break;
....
}

try not to google, no satisfying equivalent code found so far
thanks in advance
xsn
 

pingu

Well-Known Member
so please help me with this:
I need the equivalent structure in python of these C structures:

do{
// my code
}while(exp?);

I want to translate some C code to python, also I need the equivalent python code for this

switch(?){
case i: //code ; break;
....
}

try not to google, no satisfying equivalent code found so far
thanks in advance
xsn
those constructs don't really exist in python but, as usual, they are all syntactic sugar for something else.
  • do ... while can be implemented either with a loop or a tail recursive function
  • the switch is implemented using a dictionary and function calls or multiple if...elif...else.
By the way, translating literally from one language to another is a very bad idea.
 

ExSan

Active Member
those constructs don't really exist in python but, as usual, they are all syntactic sugar for something else.
  • do ... while can be implemented either with a loop or a tail recursive function
  • the switch is implemented using a dictionary and function calls or multiple if...elif...else.
By the way, translating literally from one language to another is a very bad idea.
and the python code is ... ?
 

pingu

Well-Known Member
and the python code is ... ?
where is the code to translate?

you can do a while loop like:
Code:
...
while True:
  # do something
  if condition: break
or
Code:
...
def tailrec(*args, **kwargs):
  """Tail recursive method"""
  if cond: return
  #do stuff
  tailrec(...)
For the switch:
Code:
...
switch_dict = {  key1: func1, key2: func2, key3: func3 }
switch_dict[value](*args, **kwargs)
or
Code:
...
switch_result = {
  cond1: func1,
  cond2: func2,
  cond3: func3
}[1]
or
Code:
...
if cond1:
  ...
elif cond2:
  ...
elif cond3:
  ...
else:
  ...
Although you can also have ranges in your conditions and
 

ExSan

Active Member
Thanks for your reply.
what about

do{
//...
}while();


?

it differs from while(){}
 

ExSan

Active Member
switch(exp){
case i_th: // code ; break;
}

is usually used when several branches are required, (more than 2).

My C code uses switch statement for +10

case i_th: // code; break;

how to code switch() C-instruction in the most simmilar way in python ?
 

tfors

Active Member
switch(exp){
case i_th: // code ; break;
}

is usually used when several branches are required, (more than 2).

My C code uses switch statement for +10

case i_th: // code; break;

how to code switch() C-instruction in the most simmilar way in python ?
Why are you asking? Obviously Python doesn't do everything C does exactly in the same way, or vice versa.
 

alain

Older and Wiser
Thanks for your reply.
what about

do{
//...
}while();


?

it differs from while(){}
it's the same thing, for and do...while are syntactic sugar around a while loop. @pingu made it clear before. I don't get your point.
 
Top