r/HowToPython • • Apr 07 '23

How to write a loop in ChinesePython: learning Chinese by programming

2 Upvotes

This is a ChinesePython, or zhpy, tutorial. The next step after a modest hello world is writing a loop, so here is how to do it in zhpy. The following code is a simple example, and also includes how to define a function in Chinese code. The characters used in the code are broken down into each Chinese word with pinyin and meaning.

# coding = utf-8

# by jiahua huang

姓名表 = ('张三', '李斯', '王海', '荷花')

定义 打印姓名表():

计数 = 1

取 姓名 在 姓名表:

打印(计数, 姓名)

计数+=1

主程序:

`打印姓名表()`

Let's break down each line of code into the words used. If the program in chinese above looks intimidating (it's gonna be easy!), you can check and intro to ChinesePython, that is, a hello world program in this link:

https://chinesememe.substack.com/i/103754530/chinesepython

姓名表 = ('张三', '李斯', '王海', '荷花')

[Xìngmíng biǎo = ('Zhāngsān', 'Lǐsī', 'Wánghǎi', 'Héhuā')]

The variable "姓名表" (xìngmíng biǎo), which means "name list" in Chinese, is assigned a tuple containing the strings '张三' (zhāngsān), '李斯' (lǐsī), '王海' (wánghǎi), and '荷花' (héhuā), four strings representing names.

The remaining lines of code define a function called "打印姓名表" (dǎyìn xìngmíng biǎo), which prints the names in the tuple along with a number indicating their position in the list. The function is then called in the main program, which results in the output of the names in the tuple.

So here are the words used in the other lines:

定义 打印姓名表():

[Dìngyì dǎyìn xìngmíng biǎo()]

Defines a function named "打印姓名表" (dǎyìn xìngmíng biǎo), which takes no arguments, and means "print name list". "定义" (dìngyì) means "define" in English. In ChinesePython, it is used to define a function or a variable. In this case, it is used to define the function "打印姓名表", which is called later in the main program to print the names in the tuple.

计数 = 1

[Jìshù = 1]

The variable "计数" (jìshù), meaning "count", is assigned the value of 1.

取 姓名 在 姓名表:

[Qǔ xìngmíng zài xìngmíng biǎo:]

For each "姓名" (xìngmíng, "name") in the "姓名表" (xìngmíng biǎo, meaning "name list"), do the following. This line initiates a loop that iterates through each name in the tuple "name list". The word "取" (qǔ) is a loop keyword that is commonly used in Chinese programming languages to mean "for each" or "iterate over". 在 zài means "at" or "in".

打印(计数, 姓名)

[Dǎyìn (jìshù, xìngmíng)]

"打印" (dǎyìn) means "print" in English, note that it was used previously, not as a programming keyword, but for arbitrarily naming the function, "print-name-list". In this line, the function "打印" is called with two arguments: "计数" (jìshù, meaning "count") and "姓名" (xìngmíng, meaning "name"). The variables "计数" and "姓名" are printed to the console as the loop iterates through the names in the tuple. The first argument, "计数" is the current count or index of the iteration, while the second argument "姓名" is the name being printed at that index.

The next line has characters already seen previously. This time it uses the operator "+=" (jiādēngyǐ, meaning "plus-equals") a shorthand operator in Python that adds the value on the right-hand side of the operator to the variable on the left-hand side, and then assigns the result back to the variable on the left-hand side. So "计数" (jìshù, meaning "count") is incremented by 1 each time the loop iterates. This is equivalent to the longer form: "计数 = 计数 + 1".

Finally, since the last line of the program is also already covered, that is, it is simply a call to the function which we studied on the function definition line earlier (see above), the last line below is the "main" declaration of a python program, and ends this tutorial. Thank you for making this far!

主程序:

[Zhǔ chéngxù]

"主程序" (zhǔ chéngxù, meaning "main program") is a label or marker that signifies the beginning of the main program. In Python. The "main" program is the code that is executed when the script is run. So the "主程序" line does not outright do anything in the code, but indicates the start of our main program.


r/HowToPython • • Apr 06 '23

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Mar 30 '23

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Mar 21 '23

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!


r/HowToPython • • Jan 26 '23

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Jan 17 '23

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!


r/HowToPython • • Jan 12 '23

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Jan 05 '23

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Dec 15 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Dec 01 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Nov 29 '22

I made a Python package that can automatically spin up cloud infrastructure to transcribe your voice notes in Notion

Thumbnail self.python
2 Upvotes

r/HowToPython • • Nov 24 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Nov 22 '22

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!


r/HowToPython • • Nov 10 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Nov 08 '22

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!


r/HowToPython • • Nov 03 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Oct 27 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Oct 04 '22

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!


r/HowToPython • • Sep 27 '22

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!


r/HowToPython • • Sep 22 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Sep 15 '22

Requests

2 Upvotes

Are there any tutorials or projects you'd like to see done and explained in Python? Drop them here!


r/HowToPython • • Aug 26 '22

I wrote a quick tutorial on extracting topics from a Conan O'Brien podcast (link in comments)

2 Upvotes

Hey fellow Pythonistas!

I’ve been fascinated by Machine Learning and Topic Detection with Python lately 🤪, so I wrote a short blog post about it. In the post, I’m transcribing a podcast using a speech-to-text Python SDK and deriving a list of topics from it to quickly discover, well, the topic of it! 😄

Please let me know if you need support when trying out the code. I’m happy to walk you through it or explain something in more detail.

Link to the tutorial is in the comments.


r/HowToPython • • Aug 23 '22

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!


r/HowToPython • • Aug 20 '22

network tracking with Python video

Thumbnail
youtu.be
2 Upvotes

r/HowToPython • • Aug 16 '22

New Members Intro

2 Upvotes

If you’re new to the community, introduce yourself!

  1. What's your Python Level?

  2. What brought you here?

  3. Are you looking for a mentor? Are you looking to be a mentor?

  4. A fun fact!