We've summarized what you've learned in the Python installation and the official Python tutorial.
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Check the version of Python that can be installed with pyenv.
pyenv install --list
Install an installable version of Python.
pyenv install 3.8.5
Switch to the version of Python you want to use.
pyenv local 3.8.5
pyenv global 3.8.5
zsh does not start pyenv
https://qiita.com/hidekingerz/items/e2b662dfbeeb691999de
I get a pyexpat error in pyenv on MacOS
https://www.nozograph.com/2019/11/29/macos%E3%81%AEpyenv%E3%81%A7pyexpat%E3%81%AE%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%8C%E5%87%BA%E3%82%8B%E5%AF%BE%E5%87%A6/
print(1 + 1)
print(2 - 0.1)
print(4 * 2)
print(5 / 2)
#output
# 2
# 1.9
# 8
# 2.5
You can get the character by specifying the index (subscript) of the character string. The index of the first character will be 0. You can also specify a negative value for the index. You can also get a substring.
word = 'Python'
print(word[0])
print(word[5])
print(word[-1]) #Negative value in index
print(word[0:2]) #Substring
#output
# 'P'
# 'n'
# 'n'
# 'Py'
You cannot change Python strings. Therefore, if you specify an index as a character string and assign it, an error will occur.
word = 'Python'
word[2] = 'T'
#output
# TypeError: 'str' object does not support item assignment
a = "aaa"
print(f"I{aaa}is"
The list can contain elements of different types.
list = [1, 'test', {"1":"test"}]
Lists also support concatenation and more.
squares = [1, 4, 9, 16, 25]
print(squares + [36, 49, 64, 81, 100])
#output
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
list = [1,2,3,4,5,6]
key1 = 10
key2 = 4
if key1 in list:
print(key1)
elif key2 in list:
print(key2)
else:
print('Could not be located.')
#output
# 4
Iterates List and character strings in order.
word = 'Python'
for num in word:
print(num)
#output
# P
# y
# t
# h
# o
# n
for i in range(5):
print(i)
#output
# 0
# 1
# 2
# 3
# 4
def test(a,b):
return a+b
print(test(1,3))
#output
# 4
A function that is used only once. It can be used by adding the keyword lambda.
print((lambda a,b: a+b)(1,3))
#output
# 4
All elements of List can be processed using the for statement. You can also use the if statement to narrow down the list to be extracted.
nums = [1,2,3,4,5]
nums_after = [i * 3 for i in nums if i != 2]
for num in nums_after:
print(num)
#output
# 3
# 9
# 12
# 15
#By the way, C#With LINQ, it looks like this:
# var nums = new List<int>(){1,2,3,4,5};
# var nums_after = nums.Where(x => x != 3).Select(x => x * 3);
dic = {'one':'test1','two':'test2'}
##for statement using key method
for key in dic.keys():
#Take out the dic key
print(key)
#output
# one
# two
##for statement using value method
for value in dic.values():
#Take out the value of dic
print(value)
#output
# test1
# test2
##for statement using item method
for key,value in dic.items():
#Extract the value of dic
print(key,value)
#output
# one test1
# two test2
It is similar to List, but it cannot be changed once it is assigned, and its elements are accessed faster than List.
str_tuple = (1, 2, 3, 4)
It is similar to List, but unlike List, it has no order and duplicates are ignored. You can also union and intersection.
str_set = {'red', 'red', 'blue', 'green', 'yellow', 'green'}
str_set2 = set('abcdefg')
str_set3 = set('acg')
print(str_set)
print(str_set2 | str_set3)
print(str_set2 & str_set3)
print(str_set2 - str_set3)
#output
# {'blue', 'red', 'yellow', 'green'}
# {'g', 'b', 'e', 'f', 'a', 'd', 'c'}
# {'a', 'g', 'c'}
# {'d', 'b', 'e', 'f'}
Recommended Posts