Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python #define equivalent

I'm developing a Hebrew python library for my toddler, who doesn't speak English yet. So far I've managed to make it work (function names and variables work fine). The problem is with 'if', 'while', 'for', etc. statements. if this were C++, for ex., I'd use

#define if אם

are there are any alternatives for #define in Python?

****EDIT***** For now a quick and dirty solution works for me; instead of running the program I run this code:

def RunReady(Path):
    source = open(Path, 'rb')
    program = source.read().decode()
    output = open('curr.py', 'wb')

    program = program.replace('כל_עוד', 'while')
    program = program.replace('עבור', 'for')
    program = program.replace('אם', 'if')
    program = program.replace(' ב ', ' in ')
    program = program.replace('הגדר', 'def')
    program = program.replace('אחרת', 'else')
    program = program.replace('או', 'or')
    program = program.replace('וגם', 'and')
    output.write(program.encode('utf-8'))
    output.close()
    source.close()
    import curr

current_file = 'Sapir_1.py'
RunReady(current_file)
like image 594
matanso Avatar asked Jan 25 '15 16:01

matanso


People also ask

What is used Python for?

Besides web and software development, Python is used for data analytics, machine learning, and even design. We take a closer look at some of the uses of Python, as well as why it's such a popular and versatile programming language.

Is Python OK for beginners?

Python is actually one of the best programming languages for beginners. Its syntax is similar to English, which makes it relatively easy to read and understand. With some time and dedication, you can learn to write Python, even if you've never written a line of code before.

What is A += in python?

The plus-equals operator += provides a convenient way to add a value to an existing variable and assign the new value back to the same variable. In the case where the variable and the value are strings, this operator performs string concatenation instead of addition.

Is Python written in C?

Python is written in C (actually the default implementation is called CPython).


2 Answers

Python 3 has 33 keywords of which only a few are used by beginners:

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Given that Python doesn't support renaming keywords it's probably easier to teach a few of these keywords along with teaching programming.

like image 184
Simeon Visser Avatar answered Oct 22 '22 04:10

Simeon Visser


How about if you add the #define stuff then run the c preprocessor (but not the compiler) which will give you a python source.

like image 31
Marichyasana Avatar answered Oct 22 '22 03:10

Marichyasana