Python formatted output and operators

Source: Internet
Author: User

Dictionary set list >>> dic={' Egon ': [' 123 ', 3], ' Alex ': [' alex3714 ',3]}>>> dic{' Egon ': [' 123 ', 3], ' Alex ': [' alex3714 ', 3]}>>> dic[' Alex ' [' alex3714 ', 3]>>> dic[' Alex '][0] ' alex3714 ' list set dictionary >>> user_ info=[{' Egon ': ' 123 '},{' Alex ': ' 123 '},{' Wupeiqi ': ' 123 '}]>>> user_info[{' Egon ': ' 123 '}, {' Alex ': ' 123 '}, {' Wupeiqi ': ' 123 '}]>>> user_info[1]{' Alex ': ' 123 '}>>> user_info[1][' Alex '] ' 123 ' Multiple nested types 1, two list nesting > >> users=[[' Egon ', ' 123 '],[' Alex ', ' 123 ']]>>> users[[' Egon ', ' 123 '], [' Alex ', ' 123 ']]>>> users [0] [' Egon ', ' 123 ']>>> users[0][1] ' 123 ' 2, list nested dictionary >>> users=[{' Egon ': ' 123 '},{' Alex ': ' alex3714 '}]> >> users[1]{' Alex ': ' alex3714 '}>>> users[1][' Alex '] ' alex3714 ' 3, dictionary >>> users={' Egon ': ' 123 ', '                      Alex ': ' alex3714 '}>>> users[' Alex '] ' alex3714 ' >>> ' Alex ' in users # #确认alex是否是users的keyTrue # #存在就显示Ture # # # #格式化输出1, the best way to use >>> name= ' Egon ' >>> print (' My name is ' +name ' My name isegon>>> print (' My name was ' +name) My name is Egon because of the creation of a new Module 2, >>> name= ' eg On ' >>> print (' My name was ', name) My name is egon##########%s############% represents a placeholder s for a string >>> name= ' Egon ' >>> age=18>>> print (' My name is:%s My-is%s '% (name,age)) #前面用了几个占位符后面也要用几个占位符占位符多用% () My name I S:egon my age is 18############################ #格式化输出练习现有一个练习需求. The user's name, age, work, hobby, and then print to the following format------------info of Alex Li-------------Name:alex liage:22job:teacherhobbie:girl------ -------End------------------------users=[{' username ': ' Alex ', ' age ': ' + ', ' job ': ' Teacher ', ' hobbie ': ' Girl '},{' Username ': ' Egon ', ' age ': ' + ', ' job ': ' Teacher ', ' hobbie ': ' Study '},]msg= '------------info of%s-------------Name:% SAge:%sjob:%shobbie:%s-------------end------------------------'% (users[1][' username '],users[1][' username '],u sers[1][' age '],users[1][' job '],users[1][' Hobbie ']) print (msg) ######### #改变类型 ###############>>> age=input(' Your name: ') your name:18>>> print (Age,type (age)) <class ' str ' > #显示的是字符串, we want to see that it is an integer >>> age= Input (' Your name: ') your name:16>>> age=int (age) >>> Print (Age,type (age)) <class ' int ' >##### # # # #上面的操作太复杂, it is easier to write out ################ #age =int (Input (' Your Age: ')) print (Age,type (age) >>> age=int (Input (' Your age: ') your age:18>>> print (Age,type (age)) <class ' int ' > instead converts it to a string of STR () ##################### # # #运算符 >>> print (Age,type (age)) <class ' int ' >>>> x=10>>> y=20>>> z=x + y >>> z30>>> 10-30-20>>> 10*30300>>> 2/50.4>>> 3/50.6>>> 3//50 >>> 5/31.6666666666666667>>> 55>>> 5//3 only takes integer part 1>>> 5%32>>> 2**3 2 of 3 Square 8 ############# #比较运算 >>> x=10>>> y=10>>> ID (x), type (x), X (1712829792, <class ' int '), 10 ) >>> ID (y), type (y), X (1712829792, <class ' int '), 10) integer is small so id equal x is ytrue so x is y################ #整数比较大的时候 >>> x= 222222222222222222222222222222222222222222222>>> y=222222222222222222222222222222222222222222222> >> ID (x), type (x), X (38291232, <class ' int ';, 222222222222222222222222222222222222222222222) >>>   ID (y), type (y), Y (38291088, <class ' int ';, 222222222222222222222222222222222222222222222) ID value is different The system will put the larger number in a different location x is Yfalsex = = Yture But the two numbers are equal ################ #不等于! =>>> x= ' A ' >>> y=1>>> x = yfalse>>> x! = ytrue greater than >>> x=2>>> y=1> >> 2 > 1True less than >>> x=1>>> y=2>>> x < ytrue>=>>> x=1>>> Y=1 >>> x >= ytrue#################>>> ' A ' > ' B ' false>>> ' Asdsdsada ' > ' B ' false>> > ' ABCSADSSD ' < ' B ' true>>> ' Absadsdsad ' < ' AA ' falsea-z z max a minimum first bit equal then second bit ############################ Assignment arithmetic >>> count=1>>> count+=1>>> count2>>> num=10>>> num//=3>>> num3>>> num**=2>>> num9################### ########## #逻辑运算 >>> True and truetrue>>> False and truefalse>>> 1 > 0 and < 20true>& Gt;> >>> True or falsetrue>>> >>> not True or falsefalse>>> user={' a ': 1}>>& Gt ' A ' in usertrue>>> not ' a ' in userfalse>>> ' a ' not in Userfalse

  

Python formatted output and operators

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.