Learn about the various types of data used in Python programs and how to use variables to represent this data in your programs.

Start from the hello_world. Py

  • Create a Python file:hello_world.py.
  • .pyIndicates that this is a Python file, so the editor will run it using the Python interpreter.
  • The Python interpreter reads the entire program and determines the meaning of each word in it.

variable

message = 'Hello Python world! '
print(message)
Copy the code
  • Added a name namedmessageThe variables.
  • Each variable points to a value — the information associated with that variable.
message = 'Hello Python world! '
print(message)

message = 'Hello Python Crash Course world! '
print(message)
Copy the code
  • Modify thehello_world.py, causing it to print another message.
  • The value of a variable can be changed at any time in the program, and Python will always record the most recent value of the variable.

Naming and using variables

  • Variable names can contain only letters, digits, and underscores.
  • Variable names cannot contain Spaces, but words can be separated by underscores.
  • Do not use Python keywords and function names as variable names, that is, do not use words that Python reserves for special purposes, such asprint.
  • Variable names should be short and descriptive.
  • Be careful with lowercase L and uppercase O, as they can be mistaken for numbers 1 and 0.

For now, use lowercase Python variable names. Although the use of uppercase letters in variable names does not cause errors, uppercase letters have special meaning in variable names.

Avoid naming errors when using variables

The variable is the tag

  • Variables are often described as boxes that can be used to store values, but they do not accurately describe the way variables are represented inside Python.
  • A better definition is that a variable is a label that can be assigned to a value, or that a variable refers to a particular value.

string

  • stringIt’s a series of characters. Python quotes are strings, and they can be single or double quotes.

Change the case of a string using methods

name = 'ada lovelace'
print(name.title())
Copy the code
  • methodstitle()Display each word in uppercase, that is, capitalize the first letter of each word.
  • There are several other case handling methods that are useful. For example, to change a string to all uppercase or all lowercase:
print(name.upper())
print(name.lower())
Copy the code

Use variables in strings

  • Sometimes, you might want to use the value of a variable in a string.
first_name = 'ada'
last_name = 'lovelace'
full_name = f"{first_name} {last_name}"
print(full_name)
Copy the code
  • To insert the value of a variable in a string, precede the quotation mark with a letterfThe variable to be inserted is enclosed in curly braces.
  • This way, when Python displays a string, each variable is replaced with its value.
  • useF stringMany tasks can be done, such as creating a complete message with the information associated with a variable, as follows:
first_name = 'ada'
last_name = 'lovelace'
full_name = f"{first_name} {last_name}"
print(f"{full_name.title()}")
Copy the code

The f string was introduced in Python 3.6. If you are using Python 3.5 or earlier, use the format() method instead of this F syntax.

  • To use the format() method, list the variables you want to use in the string in parentheses. Each variable is referenced by a pair of curly braces. This replaces the curly braces in order with the values of the variables listed in parentheses, as follows:
full_name = "{} {}".format(first_name, last_name)
print(full_name)
Copy the code

Use tabs or newlines to add whitespace

  • tabs\t
  • A newline\n

Delete the blank

  • Removing unwanted white space from user input data in Python is as easy as pie.
  • Python can find stringsAt the beginningAt the end ofExtra white space. Make sure that the stringAt the end ofThere is no whitespace to use methodsrstrip().
>>> favourate_language = 'python '
>>> favourate_language
'python '
>>> favourate_language.rstrip()
'python'
>>> favourate_language
'python '
>>>
Copy the code

This deletion is only temporary; to permanently remove whitespace from the string, the result of the deletion must be associated with the variable.

>>> favourate_language = favourate_language.rstrip()
>>> favourate_language
'python'
>>>
Copy the code
  • You can also remove whitespace at the beginning of the string, or on both sides of the string. For this, methods can be used separatelylstrip()strip().

Avoid syntax errors when using strings

Grammatical errors are errors that you will encounter from time to time. Syntax errors occur when programs contain illegal Python code.

The number

The integer

  • In Python, you can add, subtract, multiply and divide integers.
  • Python uses two multiplication signs to represent power operations.
  • Python also supports ordering of operations, so multiple operations can be used in the same expression. You can also use parentheses to change the order of operations so that Python executes operations in the order you specify.

Floating point Numbers

  • Python calls all numbers with a decimal point floating point.

Integers and floating point numbers

  • When you divide any two numbers, the result is always a floating-point number, even if both numbers are integers and divisible.
  • In any other operation, if one operand is an integer and the other is a float, the result is always a float.

Underscores in numbers

  • When writing large numbers, use underscores to group the numbers to make them easier to read.
>>> universe_age = 14 _000_000_000
>>> universe_age
14000000000
>>>
Copy the code
  • This is because Python ignores underscores when storing such numbers.

Assign values to multiple variables at the same time

>>> x, y, z = 0.0.0
Copy the code

constant

  • A constant is similar to a variable, but its value remains constant throughout the life of a program.
  • Python has no built-in constant type, but Python programmers use all uppercase to indicate that a variable should be treated as a constant and its value should always be the same:
MAX_CONNECTIONS = 5000
Copy the code
  • In the code, indicate that a particular variable should be treated as a constant, with all uppercase letters.

annotation

Zen Python

>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is  better than complicated. Flat is better than nested. Sparse is better than dense. 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. 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. 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! >>>Copy the code