489 views|2 replies

6027

Posts

6

Resources
The OP
 

"Python Programming Quick Start" 2. Learn Python Basics through Experiments [Copy link]

The Python programming language has many syntax structures, standard library functions, and interactive development environment features. First learn some basic programming concepts and verify them through practice. You can use the console, or write a file and then run it to view the results.
expression:
Expressions are the most basic programming construct in the language, and Python has a large number of other operators that can be used in expressions. For example, the following is a list of all Python's mathematical operators in order of precedence from highest to lowest: exponentiation, modulo/remainder, integer division/quotient, addition, subtraction, multiplication, and division. The order of operation (also called "precedence") of mathematical operators is similar to that in mathematics. The ** operator is evaluated first, followed by the *, /, //, and % operators, from left to right. The + and - operators are evaluated last, also from left to right. Parentheses can be used to change the usual precedence if necessary.
Let's test it with specific code:
x = 10
y = x**3
print(y)
y = x%3
print(y)
y = x//4
print(y)
y = x/3
print(y)
y = x+4
print(y)

The running results are as follows:
Integer, floating point, and string data types
An expression is a combination of values and operators that evaluates to a single value. A data type is a class of values, and each value belongs to exactly one data type.
Python text can also be called "strings" or strs, and single or double quotes are used to surround the string (such as 'Hello' or "Goodbye cruel world!") to indicate the beginning and end of the string. A string with no characters is called an "empty string".
"+" can represent the addition operator or the string concatenation operator, for example:
x = 'Hello'
y = "Goodbye cruel world!"
print(x+y)

The output is as follows:

Likewise, "*" has two functions, and the operator can only be used on two numbers (as multiplication), or a string and an integer (as a string replication operator).
variable
Variables can be copied with "=", and variable names must be valid (one word, can only contain letters, numbers, and underscores, and cannot start with a number), which is consistent with most languages. Variable names are case-sensitive, which means spam, SPAM, Spam, and sPaM are four different variables. It is a Python convention to start variables with lowercase letters.
There are several commonly used functions. print() prints out the internal string (it can also be a variable); input() is a function that waits for keyboard input. When it runs here, it will wait for input, and then press the Enter key to run; the len() function passes a string (or a variable containing a string) to find the number of characters in the string and returns just a variable; str(), int() and float() functions are used for forced conversion, and only the same type can be operated or superimposed.
The following are the operations of these functions:
x = 'Hello'
y = " world!"
print(x+y)
myName = input()
print(myName)
print(len(myName))
a = 3.5
print('a =:'+str(a))
b = '4.6'
print(a + float(b))

The result is as follows:

Latest reply

Learn the basics of Python from the experts,,,,   Details Published on 2024-4-26 07:32
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

6570

Posts

0

Resources
2
 

Learn the basics of Python from the experts,,,,

Comments

We are all beginners, let's learn and improve together  Details Published on 2024-4-26 08:24
 
 
 

6027

Posts

6

Resources
3
 
Jacktang posted on 2024-4-26 07:32 Learn Python basics from the experts,,,,

We are all beginners, let's learn and improve together

Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list