Parisgabriel
basics of getting started with Python
For
A data element used to traverse an iterative object
An iterative object is an object that gets the data element
An iterative object includes:
string str
Lists List
Tuple tuples
Dictionary Dict
Grammar:
The For variable list in iterates over an object:
L = ["A", "ABC", "China", 123]for x in L: print (x) Else: print ("End")
Description
When a break is used to end a loop inside a loop, else is no longer executed and else can be omitted
Range () function:
Range (stop) starts from zero, and adds 1 after each integer, until stop ( does not contain a stop)
Range (Star,stop[,step])
Move step from star starting each time an integer is generated, until stop (does not contain stop and step can be negative)
Help (Range) query
Role:
Used to create an iterative object (also called an integer sequence generator) that generates a series of integers
For x in range (0, ten, 3): print (x)
Description
The range function with the returned object can be used for the for statement to iterate through the functions in sequence.
For nesting:
For x in range (5): For y in range (3): print (x, y, "ABC")
Continue
Used in a loop statement (while, for) no longer executes the statement after continue in this loop
Start a new cycle again
For x in range: if x% 2 = = 0: continue print (x)
Description
Jump directly to truth expression in while and re-judge
Binds an element within an iterator object directly to the next loop again in for
Listing list:
A list is a container (a data object typically used to store a computer)
A list is a sequence that can be changed
There may be no association between elements that consist of a series of specific elements and elements
But they are sequenced by the order of the relationship
Empty list Creation Method:
L=[]
A list is an object that can be iterated:
Non-empty list creation method:
Empty list: L = [] Non-empty list: L = [1, 2, 3, 4]l = ["A", "B", "C"]l = [1, 2, ["A", "B", "C", "D"], 3, 4]
List of constructor lists:
List () equals empty lists
List (iterable) creates a listing with an iterative object
Operation of the list:
+ for stitching list
+ = is used to append data from the right-hand iteration object to the original list
* used to generate a repeating list
*=
Comparison operations for lists:
< <= > >= = = =!
Comparison rules and strings are exactly the same
List in , Notin:
Determines whether an element exists in the list if it is pure in return true otherwise false
In and Nat in return value opposite
Practice:
1. For implementation:
Enter a number w to represent the width of the square:
Such as
1 2 3) 4 5
1 2 3) 4 5
1 2 3) 4 5
1 2 3) 4 5
1 2 3) 4 5
Answer:
a = Int (input ("Plaese input at would integer:")) for X in range (1, a + 1): For y in range (1, a + 1): print (y, end = "") print ()
Rewrite the title:
1 2 3) 4 5
2 3 4) 5 6
3 4 5) 6 7
4 5 6) 7 8
5 6 7) 8 9
The first type:
a = Int (input ("Plaese input at would integer:")) for X in range (1, a + 1): For y in range (x, a + x): y, end = " ") print ()
The second type:
a = Int (input ("Plaese input at would integer:")) for X in range (1, a + 1): For y in range (x, a + x): print (y, end = "") print ()
2.
Enter any text, stored in the list L, when not enter any content directly enter after the end of the input
Print the contents of the L list
Calculate how many lines of content you have entered, and the number of characters
Answer:
L = []i = 0while True: a = input ("Plaese input at would string:") if a = = "": Break i + = Len (a) L + = [A]print (' L =:%syou has entered:%d row%d Entries character ' ' % (L, Len (l), i))
3.
Enter an integer (representing the height of the trunk)
Print out one of the following trees
Input: 2
*
***
*
*
Input 3
*
***
*****
*
*
*
Answer:
a = Int (input ("Plaese input at would integer:")) for X in range (1, A * 2, 2): Print (("*" * x). Center (A * 2)) Else: fo R S in range (1, A * 2, 2): print ("*". Center (A * 2))
4.
Arbitrarily enter an integer to determine whether the number is prime (prime)
Primes are also called prime numbers, which are divisible only by 1 and by themselves:
such as: 2 3 5 7 11 etc.
Tips:
By exclusion: Determine if x is a prime number, as long as the X is divided by: 2 3 4 5 ... -1
X is a prime number, as long as the x is not a prime number.
Answer:
a = Int (input ("Plaese input at would integer:")) for X in range (2, a): if a% x = = 0: s = "It's not a prime" bre AK Else: s = "This is a prime" print (s
5.
Figure out the number of daffodils within 100 ~ 1000 (naricissistic)
Narcissus number refers to the hundreds of 3 times plus 10 bits of 3 times plus you 3 times equals the number of the original number
153=1**3+5**3+3**3 153 370
Answer:
For x in range (+): if (x//+) * * 3 + ((x%)//) * * 3 + (x%) * * 3 = = x: print (x)
Parisgabriel:python Endless Day08