I'm an intern at Future Electronic Technology.
I am still studying, so please point out any mistakes.
Unlike c language etc., there is no need to declare when defining variables. You can start using it without a declaration if the variable conditions (starting with a non-numeric character, avoiding special words, etc.) are met.
In python, {} is not used. The contents of the for statement and if statement are distinguished by indentation.
for i in range (n): #Separated by:
print(i*i) #Processing in the for statement
input() #Processing outside the for statement
Use `print ()`
to print standard output.
print("hello")
#hello
a = 5
print(a)
#5
However, when displaying a mixture of numbers and letters, type conversion must be performed.
a = 5
print(a + "hands")
#error
print(str(a) + "hands") #Convert the number a to a string
#5hands
Use input
to accept standard input from the keyboard.
a = input()
#Type hello
print(a)
#hello
If you use input, the string will be stored, so if you want to treat it as a number, you need to perform type conversion.
When creating a function, use def
.
def sum(a, b):
return a+b
If you want to change some characters when you get the standard input with input () `` `, use
replace () `` `.
The actual usage example is as follows.
line = input()
# "co worker"Enter
line_2 = line.replace(' ', '-') #Space- (hyphen)Replace with
print(line_2)
# "co-worker"And output
For example, if you try to change the second character as shown below, an error will occur and it will not work.
string = "worm"
string[1] = "a"
print(string)
#I want to be warm
#TypeError: 'str' object does not support item assignment
In such cases, there are two possible solutions.
The contents of the string (str) cannot be changed, but the list can be changed. So you just have to convert the string to a list, change a particular character, and then convert it back to a string.
string = "worm"
stringList = list(string) #Convert to list
stringList[1] = "a" #Set the second letter to a
srting2 = "".join(stringList) #Convert list to string
print(string2)
#warm
In this example, insert the character you want to insert between the first and third characters of the character string.
string = "worm"
new_str = string[:1] + 'a' + string[2:] #Substitute the first and third characters of string and insert a between them
print(new_str)
#warm
Use set () to exclude duplicate one-dimensional lists. By using set (), it becomes a set type object with duplicates eliminated. To treat it as a list type, use list (set ()).
nums = [1, 2, 10, 1, 3, 1, 4, 2, 3]
nums2 = list(set(nums))
print(nums2)
#[1,2,10,3,4]
There are many frameworks in python that have programs for performing specific functions. By utilizing the framework, you can develop more efficiently. The python frameworks are as follows.
In the future, we'll learn more about Django.
Solve Python | HackerRank Change some characters in a string in Python --minus9d's diary Delete / extract duplicate elements from list (array) with Python | note.nkmk.me Complete version of the 2019 Python Recommended Framework! Thorough comparison of each framework! | Engineer project introduction
Recommended Posts