Python installation and basic grammar

Overview

We've summarized what you've learned in the Python installation and the official Python tutorial.

Development environment

Installation

1. Install pyenv

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

2. How to use pyenv and install python

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

Problem (see link below)

  1. zsh does not start pyenv
    https://qiita.com/hidekingerz/items/e2b662dfbeeb691999de

  2. 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/

grammar

Numerical value

print(1 + 1)
print(2 - 0.1)
print(4 * 2)
print(5 / 2)

#output
# 2
# 1.9
# 8
# 2.5

String

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

Embedded string

a = "aaa"
print(f"I{aaa}is"

list

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]

if statement

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

for statement

Iterates List and character strings in order.

word = 'Python'
for num in word:
    print(num)

#output
# P
# y
# t
# h
# o
# n

range function

for i in range(5):
    print(i)

#output
# 0
# 1
# 2
# 3
# 4

function

def test(a,b):
    return a+b
    
print(test(1,3))

#output
# 4

Anonymous function

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

Comprehension notation

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);

Other data structures

dictionary

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

Tuple

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)

set

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

Python installation and basic grammar
Python (Python 3.7.7) installation and basic grammar
Python3 basic grammar
Java and Python basic grammar comparison
Python basic grammar / algorithm
Python basic course (2 Python installation)
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Basic Python 3 grammar (some Python iterations)
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
I learned Python basic grammar
Source installation and installation of Python
Python basic grammar (miscellaneous) Memo (4)
Python installation
Python installation
Python Basic Course (14 Modules and Packages)
Basic grammar of Python3 system (dictionary)
Installation of Visual studio code and installation of python
python grammar check
Python installation and package management with pip
Elasticsearch installation and basic operation for ubuntu
Python installation (Windows)
Basic Python writing
[Basic grammar] Differences between Ruby / Python / PHP
Python basic operation 3rd: Object-oriented and class
Python grammar notes
[Python] I personally summarized the basic grammar.
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
RF Python Basic_02
Differences between Ruby and Python (basic syntax)
Basic grammar of Python3 system (included notation)
Python installation 2020 (macOS)
Python3.4 installation notes
BESS Development Memo # 01: BESS Installation and Basic Usage
Instant method grammar for Python and Ruby (studying)
Installation of Python3 and Flask [Environment construction summary]
VBA user tried using Python / R: basic grammar
[Python / Chrome] Basic settings and operations for scraping
Comparison of Python and Ruby (Environment / Grammar / Literal)
Basic operation of Python Pandas Series and Dataframe (1)
Python basic course (12 functions)
Python I'm also basic
Python and numpy tips
Python Basic Course (7 Dictionary)
[Python] pip and wheel
python openCV installation (memo)
Reinforcement learning 1 Python installation
[Go] Basic grammar ② Statement
Python ~ Grammar speed learning ~
Batch design and python
Python iterators and generators
jupyter and pandas installation
Python installation method Windows
[python] class basic methods
Python packages and modules