Learning Python notes --- simple data types and variables

      At the outset, this is some of the notes in personal self-study, because it is white, new to Python, previously have not had a similar experience, so things may be a lot of white level For others, there is no other meaning to write out is to organize their own records and then, by the way share it, and if the latter met could not understand, I will share it, and then ask you great God.

I was newly installed Python3, the editor is Sublime Text.

split line----------------------

The first thing I learned is:

print("Hello Python world!")

 

Then save the file name is hello_world.py, the end of the .py pointed out that this is a Python program, so the use of the Python interpreter to run it.

Preparation of procedures, the editor will highlight different parts of the program in various ways. For example: blue indicates a function name; orange Python code indicates otherwise. This feature is called syntax highlighting.

variable

The second line is the command I learned:

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

 

 

Where the "message" is a variable name, the store is "Hello Python world!" 

Each variable is stored the information --- a value associated with the variable.

Use rule variable names:

1, the variable name can only contain letters, numbers and underscores. Variable names can begin with a letter or underscore, but can not begin with a number.

2, the variable names can not contain spaces, but you can use underscores to separate words in them.

3. Do not Python key role and function names variable names that do not use the word Python reserved for special purposes.

4, the variable name that is short and should be descriptive.

5, I caution lowercase and uppercase letters O, as they may be the wrong person as the numbers 1 and 0.

 

String

 String is a series of characters. In Python, quotes are strings, which may be single quotes may be a double quote marks. such as:

'I told my friend,"Python is my favorite langusge!"'
"The language'Python' is named after Monty python,not the snake."
"One of Python's strengths is its diverse and supportive communtiy."

 

There are described in the following syntax error, or if there is a single quote double quotes in a string, it is best to enclose the string with another quotation mark.

Modify the case of the string:

.ltitle () so that the first letter capitalized string

All .upper () let a string uppercase

.lower () so that all lowercase string

E.g:

name_1="zhang\n"
name_2="\t"+name_1.lower()
name_3="\t"+name_1.upper()
name_4="\t"+name_1.title()
print(name_2)
print(name_3)
print(name_4)
Output: 
zhang ZHANG Zhang

 

 

Middle \ n newline, \ T is a blank character, the plus sign (+), is used to merge the string.

 

Delete Blank:

.rstrip () is deleted at the end of a string of blanks

.lstrip () is to remove the beginning of a string of spaces

.strip () is to remove spaces at both ends of the string

E.g:

favorite_language="   Python    "
print(favorite_language)
print(favorite_language.rstrip())
print(favorite_language.lstrip())
print(favorite_language.strip())

 

'   Python    '
'   Python'
'Python    '
'Python'

 

 

In order to look more obvious, I put the results are output in single quotation marks to enclose the.

digital

In Python, can directly integers +, -, *, / operations.

Float: Python digital with decimal points are called floating-point numbers. It noted the fact that: the decimal point may appear at any position numbers.

  .str () to convert the value to a string

Example book original is such that:

age=23
mseeage='Happy '+age+'rd Birthday!'
print(mseeage)
    mseeage='Happy '+age+'rd Birthday!'
TypeError: can only concatenate str (not "int") to str

 

 

This is being given. After modifications are:

age=23
mseeage='Happy '+str(age)+'rd Birthday!'
print(mseeage)
Happy 23rd Birthday!

 

 

 

I think the value here with a "" double quotes will be more simple, but the future may come in handy, because the fourth year, is not very understanding, looking forward to the great God of doubts.

 

Guess you like

Origin www.cnblogs.com/Slayers-Z/p/11563841.html