Python memorandum

Introduction

I'm an intern at Future Electronic Technology.

I am still studying, so please point out any mistakes.

Features of Python

Do not declare variables

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.

Don't use curly braces {}

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

Main notation

output

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

input

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.

Function declaration

When creating a function, use def.

def sum(a, b):
   return a+b

String replacement

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

Change part of the string

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.

Convert a string to a list

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

Separate the strings and insert them in between

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

Exclude duplicates from list

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]

Framework

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.

Reference URL

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

Python memorandum
Python memorandum
python memorandum
python memorandum
Python memorandum
python memorandum
Python memorandum
Python basics memorandum
Python pathlib memorandum
Python memorandum (algorithm)
Python memorandum [links]
python memorandum (sequential update)
Python
Python memorandum (personal bookmark)
Python basic memorandum part 2
[Python] Iterative processing_Personal memorandum
Memorandum @ Python OR Seminar
python memorandum super basic
Effective Python Learning Memorandum Day 15 [15/100]
Cisco Memorandum _ Python config input
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
ABC memorandum [ABC163 C --managementr] (Python)
About python beginner's memorandum function
Memorandum @ Python OR Seminar: matplotlib
[Python] SQLAlchemy error avoidance memorandum
A memorandum about correlation [Python]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Memorandum @ Python OR Seminar: Pulp
Effective Python Learning Memorandum Day 13 [13/100]
A memorandum about Python mock
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Memorandum @ Python OR Seminar: Pandas
[python] Random number generation memorandum
Effective Python Learning Memorandum Day 4 [4/100]
Memorandum @ Python OR Seminar: scikit-learn
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
python parallel / asynchronous execution memorandum
Matplotlib memorandum
linux memorandum
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
ABC memorandum [ABC159 C --Maximum Volume] (Python)
jinja2 memorandum
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Django memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
ufo-> python (3)