Coy's path to Python--day2

Source: Internet
Author: User
Tags logical operators

Section content
    1. Data type introduction and formatted output
    2. STR type
    3. STR index slicing and simple operation

First, data type introduction and formatted output

What is a data type:

We humans can easily distinguish between numbers and characters, but the computer does not, although the computer is very powerful, but from a certain point of view and very silly, unless you explicitly tell it, 1 is the number, "Han" is the text, otherwise it is not clear 1 and ' Han ' difference, therefore, in each programming language will have a called data type of East, in fact, is the common data types are clearly divided, you want the computer to do numerical operations, you will pass the numbers to it, you want him to deal with the text, send him a string type.

1, formatted output
info = "'------------info of%s-----------Name:coy yangage:100job:teacherhobbie:girl-------------End---------- -------' Print (info)

Placeholder% and double-percent

double percentif, in a string,% is a placeholder, no s or D is just as much as%
Print (' I'm%s, today%d, learning progress 2%% '% (' Coy ', 25))

Character formatting output

Placeholder

%s s = string%d d = digit integer%f f = float floating-point number, approximately equal to decimal2, more operator and logical operator precedenceThe operator is mentioned in Day1, which is today mainly about logical operatorsPrimary comparison operators and logical operatorsand as long as there is a condition of false then the result is falseor only one condition is true then the result is trueNot is the inverse of or(1) before and after comparison operators

Priority level

Print (1 > 2 and 3 < 4 or 2 > 1 and 4 < 3)
in the absence of () the not priority is higher than the And,and priority above or, that is, the priority relationship is () >not>and>orExample:
Print (1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)

(2) both front and back are valuesx or Y if x is True,return xif X or Y,x is true, then the result is X.0 is false, not 0 is true,and result is or opposite, meaning and take Y value
Print (3 or 4) print (1 or 4) print (0 or 1) print (3 and 5)

Two, str typewhat is a stringin Python, the quoted characters are considered to be strings! 1, use of quotation markssingle quotes and double quotes, when to use single quotes, when to use double quotes, three quotes
S1 = ' yangchen ' s2 = "yangchen" msg = "My name is Coy, I ' m years old!" Print (msg)

Three quotes are usually quoted in a long string

msg = "" Today I want to write a poem, praise my deskmate, you see his black short hair, like a fried chicken. "Print (msg)

2, String +*

(1) +

S1 = ' Coy ' s2 = ' ssss ' Print (S1+S2)

(2) *

S1= ' Coy ' Print (s1*8)

Third, str index slicing and simple operation

1, String index slice

Through the index cut out is a new string, the new memory address, in the fetch time to take one more, because Gu Tou regardless of the tail

s = ' Coysaihanhan ' S1 = s[0]print (S1) #获取字符串索引为0的 s = ' coysaihanhan ' s2 = s[2]print (s2) #获取字符串索引为2的 s = ' Coysaihanhan ' s3 = s[- 1]print (S3) #从后往前, take the penultimate first s = ' Coysaihanhan ' s4 = S[-2]print (S4) #从后往前, take the second last s = ' Coysaihanhan ' S5 = S[0:3]print (S5) # From the beginning of index 0 to the index of 2, because Gu Tou disregard tail s = ' Coysaihanhan ' S5 = S[0:4]print (S5) #从索引为0的开始取到索引为3的, because Gu Tou disregard tail s = ' Coysaihanhan ' s5 = S[:4]print (S5) #从索引为0的开始取到索引为3的, because Gu Tou regardless of the tail,: Before the default is the first s = ' Coysaihanhan ' S5 = S[4:9]print (S5) #从索引为4的开始去到索引为8的, because Gu Tou disregard the tail s = ' Coysaihanhan ' S5 = S[:]print (S5) #从头到尾 #可以加步长

  

  

  

  

  

  

  

  

  

  

  

  

Coy's path to Python--day2

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.