Learn to summarize the basics of--python

Source: Internet
Author: User

First of all, of course, first Hello world!.

Print (' Hello world! ')

Operation Result:

Variable

name = ' Xiaoming ' #定义一个变量

Age = 18

Input (' Please enter your name: ')

Print (name)

Operation Result:

Conditional judgment

#if Else

If 1>2:
Print (' That's impossible ~ ')

Elif 1=2:

Print (' also impossible ~ ')
Else
Print (' Yes! ‘)

String formatting

Import datetime
user = ' Wangjian '
Today = Datetime.datetime.today () # get todays date
# print (type user) output user type
# print (today) output today's type
Today = STR (today) #转换类型
#msg = ' welcome ' +user+ ' presence, today's date is ' +today ' define MSG variable
msg = ' Welcome%s ' login, today's date is \t%s\n '% (user,today) #%s---placeholder; \ n---line break; \ t---whitespace
Print (msg)

#############################################################################################

Age = 18
Score = 98.5326
Print (Round (score,2)) # round () keep several decimal places
msg = ' Your age is%d, your score is%.2f '% (age,score) # String%d integer%f is a decimal. 1 Keep one decimal place,. 2 Reserve two decimal places
Print (msg)

Loop, iterate, iterate

# while

Here, give me a chestnut! Guess number Game ~

Import Random
num = random.randint (1,100) #产生一个1-random number between 100
Count = 0 #定义一个变量用于计数

########## #循环体 ############
While count<7: #最多猜7次
Guess = input (' Please enter the number you guessed: ')
guess = Int (guess)
If guess > num:
Print (' Big up ')
# continue# ends this cycle, loops from the beginning, and does not perform the subsequent count=count+1
Elif Guess < num:
Print (' small ')
    # continue
Else
Print (' Congratulations on your guess right ')
Break #立即结束循环
Count = Count + 1
else: #循环正常结束之后, the else is executed
Print (' Game over ')

########## #循环体 ############

#count +=1
#count-=1 #count = count-1
#count *=1 #count = count*1
#count/=1 #count = COUNT/1

#for

num = 10
For I in range (3):
Guess = input (' Please enter the number you guessed: ')
guess = Int (guess)
If guess > num:
Print (' Big up ')
Elif Guess < num:
Print (' small ')
Else
Print (' Congratulations on your guess right ')
Break #立即结束循环
else: #循环正常结束之后, the else is executed
Print (' Game over ')

List array

List array
Stus = [' Little black ', ' Little white ', ' Little Red ', ' Little Blue ']
# 0 1 2 3
#下标, angle, index, number
Print (Type (stus))
Print (Stus[0])
#增加
Stus.append (' Little black ') #在列表末尾增加一个元素
Print (Stus)
Stus.insert (0, ' little orange ') #在指定位置添加一个元素
Print (Stus)
#删除
Stus.pop (2)#删除指定位置的元素
Print (Stus)
Stus.remove (' Little Red ')#删除指定的元素
Print (Stus)
Del Stus[1]#删除指定位置的元素
Print (Stus) s
#修改
STUS[1] = ' small ash '
Print (Stus)
#查询
Print (Stus[-1])#取值 Subscript-1 takes the last element
Stus.clear ()#清空整个list
Print (Stus.count (' small black '))#统计这个元素在list里面出现了几次
Count = Stus.count (' Little Black ')
Print (count)
Print (Stus.index (' small black '))# Returns the index of the first occurrence of this element, or an error if the element does not exist in the list .
Print (Stus)
Stus.reverse ()#反转
Print (Stus)
Stus2 = [' ingot ', ' and two ', ' egg hemp ']
Print (Stus2+stus)
Stus.extend (STUS2)#把后面list里面的值, add to the first list
Print (Stus)
Stus3=stus+stus2#合并两个list
Print (STUS3)

Nums = [1,3,5,7,9,5,6,7,9,10]
Nums.sort () #排序, the default is ascending
Nums.sort (reverse=true) #降序
Print (Nums)

n = [a] #一维数组
N2 = [[1,2,3],[4,5,6]] #二维数组
my = [
[1,2,3,4,5,6],
[' Name ', ' age ', ' sex ', ' haha ', [' xiaoming ', ' small aperture ', ' Xiao Qiang '],
890
] #三维数组
Print (my) #看变量的元素个数, length
Print (my[1][4][0]) #找小明
My[1][4].append (' Floret ') #在小强后面加一个小花
Print (My[1][4])
my[1][2]= ' sex ' #将sex改为性别
Print (my)

Determine if you are in list: two methods
Username = input (' User: ')
Count = Stus.count (username)
Print (Stus)
If count>0:
Print (' The user already exists ')

Learn to summarize the basics of--python

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.