What is the difference between an expression and a function in Python?

Change: the proposed duplicate does not answer my question, since I am primarily concerned with the differences in Python specifically. The proposed duplicate is much broader than this question.

I recently started learning Python. I am currently reading "Learn Python the Hard Way." I have some experience with special programming, but I will return to the beginning to learn everything from scratch this time.

One of the first lessons in the book is concerned print, and the author gives various instructions for using it in Python 2.7, for example:

print "This is fun."

I was wondering what is technically called printhere in terms of programming. Some studies have found this, PEP-3105

In this case, it is done in order to make the printfunction:

The printed expression has long appeared in lists of dubious language functions that should be removed in Python 3000, such as the Guido Presentation of "Python Regrets" 1 . Thus, the goal of this control panel is not new, although it may become a subject of debate among Python developers.

Thus, it printis an operator in Python 2.7 and a function in Python 3.

But I could not find a clear definition of the difference between statementand function. I also found this by the person who invented Python, Guido van Rossum, in which he explains why it would be nice to make print a function instead of an expression.

, , , - , . print Python 2.7? ?

Python?

+11
2

- . - . , def:

def Spam(): pass

, Python, , . , .

+4

Python - , . , . , (""). , . "" "" .

, - :

5 + 3 # This statement adds two numbers and returns the result
"hello " + "world" # This statement adds to strings and returns the result
my_var # This statement returns the value of a variable named my_var
first_name = "Kevin" # This statement assigns a value to a variable.
num_found += 1 # This statement increases the value of a variable called num_found
print("hello") # This is a statement that calls the print function
class User(BaseClass): # This statement begins a class definition
for player in players: # This statement begins a for-loop
def get_most_recent(language): # This statement begins a function definition
return total_count # This statement says that a function should return a value
import os # A statement that tells Python to look for and load a module named 'os'

# This statement calls a function but all arguments must also be valid expressions.
# In this case, one argument is a function that gets evaluated
mix_two_colors(get_my_favorite_color(), '#000000')

# The following statement spans multiple lines and creates a dictionary
my_profile = {
  'username': 'coolguy123' 
}

:

first+last = 'Billy Billson'
# Throws a Syntax error. Because the plus sign is not allowed to be part of a variable name.

Python , , , . , C Java, , , (;).

Python2, Python3

print("this is a message") 

. , print, .

Python2 . , "", , . Python3 .

print "this is a message"
0

Source: https://habr.com/ru/post/1674913/


All Articles