"Python programming from entry to practice" --01 simple variables and data types

For some reason, I had to use python do develop, they begin to learn python, before the foundation can only make me read the code, and the development of simple, just now learning about the system

variable

First look at a piece of code

name = "Alice"
print(name)

We've added a name for the name of the variable whose value is "Alice", after the run, it will print out "Alice" is the word

In python end of each line of code does not need to add ';' default line is a line of code, unlike java or c ++ (that I often write python semicolon)

Another point to note is that, python no braces this thing, "block" concept is actually by indentation, that is "not arbitrarily indent"

The print output function can take any of the content, and can output a plurality of contents

message = "Hello Python Crash Course reader!"
print(message, name)

If there is a space between your content so that the output will further message content and name, if you do not want to have this space, then use the "+" connection

Of course, for the variable name, we are not at liberty to name, it must meet the following criteria:

  • Only contain letters, numbers, and the underscore, can not start with a number
  • Can not contain spaces
  • Can not be named by keyword or function name
  • The best brief and meaningful variable names, can make people understand at a glance
  • Alphanumeric caution i I l 1 O 0 o this confusingly

Of course, how to describe a variable name, function name can refer to "clean the Road Code," no matter what language you are doing development, naming is a very important part

Another point, create a variable in python is not required in advance define the variable type, that is, the variable name in subsequent use must pay attention to spelling, for example

name = "Alice"
print(nam)

Is different from java, c ++ compiler error would not have quoted wrong, it will not rosy. Therefore, the use Please note this point.

String

It refers to a series of strings of characters, in python, single lead, double lead, are caused by the strings. Unlike java and c ++, single primer is a single character.

I am here to throw a question: if this concept does not exist a single character in python

For single and double quotes cited convenience we actually use some quotes in the string, unlike java, if you want to use a string within double quotes must be escaped in java

For the case of changing the function string, provides mainly three

= address " titless Book " 
Print (address.title ())   # title to convert the first letter of each string to uppercase 
Print (name.upper ())   # turn uppercase 
Print (name.lower ())   # turn lowercase

Of course, also can be spliced ​​between the strings, with the "+" sign to splice

Note that the numbers do not directly and string concatenation, if you want to give a string to splice some numbers, but first these numbers into a string

We can also add some string tab displays output content modification newline

For a blank, sometimes very confusing, especially at the tail of the space, python from a number of functions to provide spaces

print ( " \ tPython " )   # tab 
print ( " \ npython " )   # line breaks in python print default wrap 
word1 = " python " 
word2 = " python " 
print (== word1 word2)   # python should be sure '==' string comparison is for content created by the same class object, if == comparison of what is, persistence 
word1 = word1.rstrip ()
 Print (== word1 word2) 
word3 = "   Python   " 
Print (word3.rstrip ())   #rstrip () function can only remove the tail of extra spaces, and can not all be removed 
Print (word3.lstrip ())   # lstrip () before removing extra spaces 
Print (word3.strip ())   # remove the spaces on both sides of 
Print (word3. replace ( '  ' , '' ))   # if you want to remove all, or alternatively, use replace () function 
# for the function strip series, brackets can take other parameters 
word3 = " pythonnnnnnnnnnnnnnnnnn " 
Print (word3.rstrip ( ' n- ' ) ) # will disappear at the end of a series of n all, but if the last argument is not a character that can not be eliminated, other empathy

A supplemental content is based on the Python 3.x, 2 and 3 in terms of part poles do not have a

Print example, it does not require re-bracketed 2, and a comment, if required in the Chinese 2, requires additional operating

digital

Integer, Integer can perform + - * / four sample calculation, by the way ** is in python mean power

Float, float with a decimal point are, in python should not float and double this distinction

Prior also said that if you want to use numbers when splicing, needs to be translated. Using the conversion str () function can be

It is also important. In the python3 calculates the 3/2 1.5, 3/2 in the python2 1 will be calculated. If direct conversion different from each other must be noted in detail between py 2- 3

summary

In fact, as a java developer to learn python, in fact, there are many places not used to, of course, as a non-sprouting new terms. Learning system, it is necessary to think more of, let's say I have been thinking at the time of reading, the variable proportion of the problem space. I did not see this book again, it might be the entry, not very involved in this. But for java, the former definition of variables need to define the type, then the space depends on the type of each variable in fact, python is no concept of a virtual machine, its footprint is actually a variable with the change os varies.

Guess you like

Origin www.cnblogs.com/Moriarty-cx/p/11869037.html