I wrote the basic grammar of Python with Jupyter Lab

Basic Python grammar

3+3
6
3-2
4*5
6/2
7%2 #Only the return value of the last executed row
1
type(1)
int
type(1.0)
float
type(3.14)
float
type('string')
str
'hello'[2]
'l'
'hello'[1:3]
'el'
'hello{}'.format('world')
'helloworld'
print('{one}{two}'.format(one='hello', two='world'))
helloworld

List

list_out = [1,2,'three']
list_out
[1, 2, 'three']

list_in = ['one', 'two', 3] list_out.append(list_in) print(list_out)

list_out[1]
2
list_out[3][1]
'two'
list_out = [1,2,'three']

Dictionary

dict = {'key1':'value1', 'key2':2}
dict['key1']
'value1'
dict = {'key1':[1,2,'value1']}
dict['key1'][2]
'value1'
dict = {'key_out':{'key_in':'value_in'}}
dict['key_out']
{'key_in': 'value_in'}
dict = {'key1':'value2', 'key2':2}
dict['key3'] = 'new_value'
dict
{'key1': 'value2', 'key2': 2, 'key3': 'new_value'}

Tuples

list1 = [1,2,3,4,5]
tuple1 = (1,2,3,4,5,6)
list1
[1, 2, 3, 4, 5]
tuple1
(1, 2, 3, 4, 5, 6)
list1[1] = 10 #list can be changed
list1
[1, 10, 3, 4, 5]
tuple1[1] = 10 #tuple cannot be changed
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-38-df5023792f06> in <module>

----> 1 tuple1 [1] = 10 #tuple cannot be changed

TypeError: 'tuple' object does not support item assignment

function

result_list=[] #frequent
for i in range(0,4):
    result_list.append(i*10)
result_list
[0, 10, 20, 30]
[i*10 for i in range(0,4)]
[0, 10, 20, 30]
i=0
while i<5:
    print('{} is less than 10'.format(i))
    i+=1
0 is less than 10
1 is less than 10
2 is less than 10
3 is less than 10
4 is less than 10

lambda function

def function_name (param1, param2):
    print('Do something for {} and {}.format(param1, param2)')
    
    return 'param1' + 'and' + 'param2' 
param1 = 'something1'
param2 = 'something2'
output = function_name(param1, param2)
Do something for {} and {}.format(param1, param2)
def get_filename (path):
    return path.split('/')[-1]
get_filename('/home/school/pet/corona')
'corona'
lambda path: path.split('/')[-1]
<function __main__.<lambda>(path)>
x = lambda path: path.split('/')[-1]
x('/home/school/pet/corona')
'corona'

Recommended Posts

I wrote the basic grammar of Python with Jupyter Lab
I wrote the basic operation of matplotlib with Jupyter Lab
I wrote the basic operation of Pandas with Jupyter Lab (Part 1)
I wrote the basic operation of Pandas with Jupyter Lab (Part 2)
I wrote the basic operation of Seaborn in Jupyter Lab
I wrote the basic operation of Numpy in Jupyter Lab.
[Python] I personally summarized the basic grammar.
I learned Python basic grammar
Summary of the basic flow of machine learning with Python
What you want to memorize with the basic "string manipulation" grammar of python
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I evaluated the strategy of stock system trading with Python.
I liked the tweet with python. ..
Basic grammar of Python3 system (dictionary)
I wrote the queue in Python
I wrote the stack in Python
Basic study of OpenCV with Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
[Python] I wrote the route of the typhoon on the map using folium
I want to output the beginning of the next month with Python
I wrote the code to write the code of Brainf * ck in python
I tried to improve the efficiency of daily work with Python
Build Jupyter Lab (Python) environment with Docker
I didn't know the basics of Python
Build the execution environment of Jupyter Lab
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
The Python project template I think of.
Python Basic Course (at the end of 15)
[Python beginner] I collected the articles I wrote
Basic grammar of Python3 system (included notation)
Python3 basic grammar
I replaced the numerical calculation of Python with Rust and compared the speed
Comparing the basic grammar of Python and Go in an easy-to-understand manner
I tried to get the authentication code of Qiita API with Python.
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I measured the speed of list comprehension, for and while with python2.7.
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
I checked the list of shortcut keys of Jupyter
Check the date of the flag duty with Python
Try the free version of Progate [Python I]
This is the only basic review of Python ~ 1 ~
I tried "binarizing" the image with Python + OpenCV
This is the only basic review of Python ~ 2 ~
Fill the browser with the width of Jupyter Notebook
I touched some of the new features of Python 3.8 ①
This is the only basic review of Python ~ 3 ~
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to automatically send the literature of the new coronavirus to LINE with Python