Python Crash Course Reading Notes - Chapter 2: Variables and Simple Data Types

Keywords: Python Programming Excel Red Hat

variable

In the file hello_world.py,.Py is the suffix of the python file and is therefore parsed using the Python interpreter.

$ cat hello_world.py
print("Hello Python world!")

Now change to variable form, message is variable, and each variable is associated with a value:

message = "Hello Python world!"
print(message)

Variable names need to be meaningful first, and then short.And you can't use keywords like print.Also, like license plates, avoid using l and O to avoid confusion with 1 and 0.For example, student_name is stronger than s_n and name_length is stronger than length_of_persons_name.Finally, lower case is used because upper case has a special meaning in Python.

The best way to understand new programming concepts is to try using them in your programs.

That's why learning by doing
Variables are not boxes, they are labels.Is reference, pointer.

String type

String is a series of characters, the data in quotation marks is string, and quotation marks can be single or double quotation marks.This sometimes eliminates the need for escape.If necessary, the escape character remains \.
The following uses method s, which are called through.And always followed by ().Methods can be nested.

$ cat name.py
name = "long long ago"
print(name.title())
print(name.upper())
print(name.lower())
print(name.lower().upper())
$ python name.py
Long Long Ago
LONG LONG AGO
long long ago
LONG LONG AGO

String can have variables, called f-string s, contained between {and}, similar to%s in C printf.
f-string support started after python 3.6.So you need to run with python3.

$ cat fullname.py
first_name = "ming"
last_name = "xiao"
full_name = f"Hello {first_name} {last_name}!"
print(full_name)
print(f"Hello, {full_name.title()}!")
$ python3 fullname.py
Hello ming xiao!
Hello, Hello Ming Xiao!!
$ vi fullname.py

string can include non-printable characters such as \n, \t.

>>> print "Just\tDo\tit!\n"
Just    Do      it!

Spacing handling:

>>> language=' python '
>>> lang = ' python '
>>> lang.rstrip()
' python'
>>> lang.lstrip()
'python '
>>> lang.strip()
'python'

Number type

There are integer and float.All with decimal points are floats.Number type supports addition, subtraction, multiplication and division (+-*/).
The mix of integer and float results in float, and the division of the two integers results in float.
To increase readability, numbers can be underlined (), similar to commas (,) in Excel, which python 3.6 began to support:

$ python3
Python 3.6.8 (default, Aug  7 2019, 08:02:28)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39.0.1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=1_234_567
>>> b=1_234.567
>>> a
1234567
>>> b
1234.567

Multivariate simultaneous assignment:

>>> x, y, z = 0, 0.1, "ok"

Constants, variable names are recommended in all capitals:

>>> TIMEOUT=500

Notes

Notations follow hash mark (#).
Notes are not only for your own sake, but also for others to understand:

If you want to become a professional programmer or collaborate with other programmers, you should write meaningful comments.

Notes should be clear and accurate:

Writing clear, concise comments in your code is one of the most beneficial habits you can form as a new programmer.

It is better to write a comment before deleting than not to add it later.

Python Zen ()

The Zen of Python Is a series of guidelines for writing Python code.
Enter import this to view these principles, and I put some of my explanations in the comments above them:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
# Explicit is better than guess
Explicit is better than implicit.
# Simple is better than complex
Simple is better than complex.
# If simplicity is unavailable, choose the simplest from complexity
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
# Readability counts
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
# Reuse code as much as possible, using functions
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
# Don't strive for perfection, make it possible before you improve
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Here's a special mention of Now is better than never, as Martin Fowler said:

first make it run, then make it faster

It can also be said that "a timely 80 points is better than a late 100".

352 original articles were published. 42% were praised. 550,000 visits+
His message board follow

Posted by gfadmin on Wed, 15 Jan 2020 16:49:55 -0800