Getting started with Python programming to Practice-notes (1, 2)

Source: Internet
Author: User


Self-learning Python for some time, always feel that their foundation is not strong, think of the two books to read the basis of writing a blog to do a note what.
Be prepared to take a look again at "Getting started with Python programming to practice" and stick to blogging notes.

Getting started with Python programming to the first two chapters of the practice notes, learn the following:
View the Python version in the current environment
Building a Python environment

Names of variables and variables
Printing of strings
Modify the case of a string

Tabs and line breaks
Remove white space characters
A python integer operation that calculates the square, cubic
Use the str () function

View the Python version in the current environment
Enter Python at the command line to see the Python version installed in the current environment
My host is installed on the python3,windows under the installation strongly recommended through the Anacoda to install, really convenient. (You don't need to install Python separately, just one package is all done)
If you use Python2 to install a Ubuntu16.04 under a virtual machine, the version that comes with the system is python2.

In the Windows environment


The Ubuntu Environment


Variable
Define a variable: variable – Value
Store a value in the message variable Hello Python world! and print variables
-------------------------------
Message = "Hello Python world!"
Print (message)
-------------------------------
Hello Python world!

Variables can be defined repeatedly, but only the latest values are printed
-------------------------------
Message = "Hello Python world!" Print (message) message = "Hello Python Crash Course world!" Print (message)
--------------------------------

Hello Python world!

Hello Python Crash Course world!


Name of the variable:

1) Variable names can contain only letters, numbers, and underscores, but cannot start with a number.

2) The variable name cannot have spaces and can be connected using an underscore. Example: Beyond_soft

3) You cannot use Python's keyword and function name as a variable name. Example: Print

4) Try to use less than 1 and Q, not good distinction.

String:

The quotation marks in Python are all strings, which can be single quotes or double quotes.

Single quotation mark nested double quotation marks, double quotation marks nested single quotation marks, you can output the contents of parentheses normally (including the single \ double quotation marks in parentheses)

"Hello ' Python Crash ' Course world!"
' Hello ' Python Crash ' Course world! '
"Hello Python ' Carsh Course world!"

Modify the case of a string by using a system function
. Title () capitalize the first letter of the string
---------------------
name = "Ada Lovelace"
Print (Name.title ())
---------------------
Ada Lovelace

. Upper () capitalizes all the letters of a string
----------------------
name = "Ada Lovelace"
Print (Name.upper ())
----------------------
ADA LOVELACE

. lower () All lowercase letters of the string
----------------------
Name = "ADA LOVELACE"
Print (Name.lower ())
----------------------
Ada Lovelace

Merging strings
One thing to note in the merge string is the two double quotes in the following example.
Two double quotation marks in the middle with a space, printed as Ada Lovelace
If there are no spaces, print as Adalovelace
The + number is specifically used to connect variables and characters, and will not be printed
-----------------------------------------
first_name = "Ada"
last_name = "Lovelace"
Full_name = first_name + "" + last_name
Print (Full_name)
-----------------------------------------
Ada Lovelace

Reference the system function in print () and print
-----------------------------------------
first_name = "Ada"
last_name = "Lovelace"
Full_name = first_name + "" + last_name
Print ("Hello," + full_name.title () + "!")
------------------------------------------
Hello, Ada lovelace!

Tabs and line breaks
TAB \ t: similar to Tab key
Line break \ n: Carriage return line
Note the following,
Tabs are not valid when they are in front of a line break
The tab effect is not printed because the tab is followed by a newline.
------------------------------------------------
Print ("Languages:\n\tpython\t\nc\t\njavascript")
-------------------------------------------------
Languages:

Python

C

Javascript


Remove Blank

. Rstrip () Delete the last blank line of the string

---------------------------------

favorite_language = ' python '
Print (Favorite_language)
Print (Favorite_language.rstrip ())
Print (Favorite_language)
----------------------------------

Python #最后有一个空白字符

Python #最后无空白字符

python #最后有一个空白字符


You can also use the definition variable to cancel the trailing line of white space characters
---------------------------------------------

favorite_language = ' python '
Favorite_language = Favorite_language.rstrip ()
Print (Favorite_language)
----------------------------------------------
Python #最后无空白字符

Remove Whitespace characters summary
. Rstrip () Delete the trailing blank line
. Lstrip () Delete the beginning blank line
. Srtip () Delete blank lines at the beginning and tail lines

Integer arithmetic in python
Integer arithmetic of subtraction

>>> 2 + 3

5

>>> 5–3

2

>>> 2 * 4

8

>>> 8/4

2.0

Computes the square and cubic of integers

>>> 3 * * 2

9

>>> 6 * * 2

36

>>> 3 * * 3

27

>>> 2 * * 3

8

Use the str () function to avoid error types

To print numbers you need to specify the str () type

----------------------------------------------

Age = 23
message = "Happy" + str (age) + "rd birthday!"
Print (message)
----------------------------------------------
Happy 23rd birthday!

"Getting started with Python programming to Practice" 1th, Chapter 2 concludes.
Also hope to pass by the Daniel Generous enlighten, brother here thanked.

This article is from the "LULU" blog, make sure to keep this source http://aby028.blog.51cto.com/5371905/1964603

Getting started with Python programming to Practice-notes (1, 2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.