Python function Little Exercise

Source: Internet
Author: User

Practice the function of the classmate look over
#coding =utf-8
#3.1 7 Rows 7 columns Specifies the shape of the pattern
DefGraph (N):
If n==1:
Print‘ @\ n‘*7
If n==2:
Print‘@@@@@@@‘
Print‘ @‘
Print‘ @‘
Print‘@@@@@@@‘
Print‘@‘
Print‘@‘
Print‘@@@@@@@‘
Graph1)
Graph2)
# * Number equals the number of patterns
#3.21 string list, each element is an IP for the most frequently occurring IP
list=[' 10.199.88.161 ',' 10.199.88.162 ',' 10.199.88.163 ',' 10.199.88.163 ',' 10.199.88.163 ']
dict={}
Li=[]
For IIn list:
If not Dict.has_key (i):
dict[i]=1
Else
dict[i]+=1
For IIn Dict.keys ():
If dict[i]==Max (Dict.values ()):
PrintThe most frequently seen IP is: ', I
#has_key方法的调用和字典键值对的访问

#3.3 function Write Fibonacci the tangent sequence (rabbit formula)
DefFibonaqie (n):
If n==0:
Return0
If n==1:
Return1
If n>1:
Return Fibonaqie (n2) +fibonaqie (n1)
Print Fibonaqie (5)
#深刻理解fibonaqie (2) It's easy to go.

#3.4 Write a function that identifies whether the string conforms to the python syntax variable name
Import string
Defvar (v):
If v[0]Not in String.letters:
PrintU ' This string does not conform to the rule '
ReturnFalse
If v[-1]Not IN (string.letters+string.digits):
PrintU ' This string does not conform to the rule '
ReturnFalse
For IIn v[1:-2]:
If IIn (string.letters+string.digits+‘_‘):
PrintU ' This string conforms to the rule '
ReturnTrue
Else
PrintU ' This string does not conform to the rule '
ReturnFalse
var' asda_123 ')
var' asda_123_ ')
var' 11 ')
var' *&^ ')

#3.9 write a string that computes the length of a string by traversing it
DefJlen (str):
x =0
ForIIn STR:
x+=1
PrintU ' the length of the string is: 'X
return x
Jlen (' Xiegezifuchuan ')
#3.10 writes an indefinite number of strings stitching the first and last strings
DefSplicing (*args):
Return args[0]+args[-1]
Print splicing (' 1 ',' 2 ',' 3 ')

#3.11 Define a function, enter an indefinite number of numbers, return all the numbers and
DefSumofdigits (*args):
x=0
For IIn args:
X+=i
return x
Print Sumofdigits (1,2,3,4,5)

#3.12 passing in multiple parameters, returning with List
DefReturnlist (*args):
Li=[]
For IIn args:
Li.append (i)
Return Li
Print Returnlist (1,2,3,4)

#3.13 any input month and day, calculate which day of the year this is
def Whichday (M, D):
if M in (1,3, 5,7): Return u ' This is this year ' +str ((m-1) *30+ (m-1)/2+d-2) +u ' Day ' if M in (2,4,6,8,10,12) : Return u ' This is this year's ' Str ((m-1) *30+m/2+d-2) +u ' if M in (9,11): Return u ' This is this year ' str ((m-1) *30+ (m+1)/2+d-2) +u ' Day ' print Whichday (3,25) #数学题-. -

Python function Little Exercise

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.