Try the free version of Progate [Python I]

Continuing from Last time, I will continue to do Progate free lessons.

This time it will be Python.

Inexperienced. Interpreter language. Should I do it with a glue like JavaScript? w

Python I Official lesson

Environment

Progate had an installation procedure. → https://prog-8.com/docs/python-env-win

After installation, I would like to write it in VS Code and execute it. I was allowed to reference. → Prepare Python environment with VS Code

I will return to the lesson.

String

-Routine character string output In python

print

Seems to use.

print('Hello World')

image.png

Let's calculate numerical values

・ Usual ... +-* /%

Variables ・ Let's use variables ・ Let's update the value of variables ・ Concatenation of character strings

・ The usual one ...

String concatenation

'{}_{}_{}'.format(1,2,3) # 1_2_3

Is the mainstream?

****** [You pointed out in the comments. ] *** It seems that it can be written like this now.

a, b, c = 2020,3,20
print(f'today{a}Year{b}Month{c}It's a day')

VS Code results image.png ** **

Data type

-Type conversion is required to concatenate characters and numbers. I think Ruby was the same ...? C # and java become characters without permission, though.

・ Character string with str (numerical value) ・ Numerical value with ʻint (character string) `

Conditional branch

・ Hmm. I double the equality with ==, but it is difficult to understand because there is no : or {} block. It may be difficult to understand if it becomes a little redundant or if there are many ifs.

Boolean value / comparison operator

・ As usual, omitted

else ・ elif

・ In Python, ʻelif... It gets messed up with other languages. else if elseif elsif elif`

Wow ah ah ah ah ah ah ah I was allowed to reference. → other if of major web languages

****** [You pointed out in the comments. ] *** ʻElse if is not a single conditional branch, it is just ʻelse followed by ʻif, which is different from ʻelse if and ʻelif. In Python, you can't write ʻif in one line after ʻelse`, so it will be a line break.

if a == 2:
    print('test')
else:
    if a == 3:
        print('test2')

I understand that ʻelif` is provided because the nesting becomes deep as described above.

If it were a specification that could write ʻelse ifin one line You need{}orthen end` I wonder if only one line can be written after the conditional branch. (Python can be written multiple times if the indents are aligned) ** **

Let's combine conditional expressions

・ Too unique.  &&Or||I wanted you to ...

&&and ||or !=not ==

Hmm.

** Python has ʻand and ʻor other uses **

message = "test1" and "test2" #test2 is assigned
message = "" and "test2" #""Is assigned
message = "test1" and "" #""Is assigned

message2 = "test3" or "test4" #test3 is assigned
message2 = "" or "test4" #test4 is assigned
message2 = "test3" or "" #test3 is assigned

Reason: -In the case of a character string, "" is interpreted as false, and the others are interpreted as positive.

・ ʻAnd` returns the result on the right when the expression on the left is positive → "test2" on the first expression, "" on the third expression When the left is false, the left is returned → The result of the second formula

・ ʻOr` returns the left when the expression on the left is positive → “test3” in the first expression, “test3” in the third expression When the left is false, the right is returned → "test4" in the second formula

JavaScript&&Or||It's the same as the operation of! Tabun

Let's calculate the price

·nothing special

Receive input

・ It seems that you can receive input with ʻinput. Is it Console.Read ()` in C #?

Let's make a conditional branch

** Exercise **

apple_price = 200
#Substitute the number 1000 for the variable money
money = 1000

input_count = input('Please enter the number of apples you want to buy:')
count = int(input_count)
total_price = apple_price * count

print('How many apples to buy' + str(count) + 'It is an individual')
print('The payment amount is' + str(total_price) + 'It's a yen')

#money and total_Please branch the condition according to the comparison result of price
if money > total_price:
    print('Apples' + str(count) + 'I bought one')
    print('The balance is' + (money - total_price) + 'It's a yen')
elif money == total_price:
    print('Apples' + str(count) + 'I bought one')
    print('The wallet is empty')
else:
    print('Not enough money')
    print('I couldn't buy an apple')

image.png

** Cleared ** image.png

Impressions

・ It ended up being very short ... -It's very easy to program with Ruby, but the operators and conditional branching methods are too unique, so I personally prefer Ruby.

I'd like to try machine learning someday, so I'll try Python again at that time.

Next time, I would like to do command line. → Next time

Recommended Posts

Try the free version of Progate [Python I]
[Python] Try pydash of the Python version of lodash
Try the python version of emacs-org parser orgparse
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
About the virtual environment of python version 3.7
The Python project template I think of.
I touched some of the new features of Python 3.8 ①
Try using the collections module (ChainMap) of python3
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
[Trainer's Recipe] I touched the flame of the Python framework.
I checked out the versions of Blender and Python
Let's use the Python version of the Confluence API module.
I tried to summarize the string operations of Python
Align the version of chromedriver_binary
Towards the retirement of Python2
About the ease of Python
Version upgrade of python Anaconda
Check OpenSSL version of python 2.6
About the features of Python
I downloaded the python source
The Power of Pandas: Python
[Example of Python improvement] I learned the basics of Python on a free site in 2 weeks.
Try scraping the data of COVID-19 in Tokyo with Python
Put the latest version of Python on linux (Debian) on Chromebook
I tried "gamma correction" of the image with Python + OpenCV
Try to get the function list of Python> os package
I want to specify another version of Python with pyvenv
I just changed the sample source of Python a little.
I wrote the basic grammar of Python with Jupyter Lab
I evaluated the strategy of stock system trading with Python.
I tried face recognition from the video (OpenCV: python version)
[Python] I tried to visualize the follow relationship of Twitter
Try to automate the operation of network devices with Python
I want to know the features of Python and pip
What is the default TLS version of the python requests module?
[Python] I tried collecting data using the API of wikipedia
I sent the data of Raspberry Pi to GCP (free)
[Python] Tuple version of prefecture pull-down
Test the version of the argparse module
The story of Python and the story of NaN
Raise the version of pyenv itself
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Try the Python LINE Pay SDK
I investigated the mechanism of flask-login!
Ideone> Python version: 3.5 (as of August 29, 2017)
How to get the Python version
[Python] Understanding the potential_field_planning of Python Robotics
I liked the tweet with python. ..
Review of the basics of Python (FizzBuzz)
I wrote the queue in Python
Try using the Python Cmd module
About the basics list of Python basics
I wrote the stack in Python
Learn the basics of Python ① Beginners
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
The story that the version of python 3.7.7 was not adapted to Heroku
I checked the distribution of the number of video views of "Flag-chan!" [Python] [Graph]
I compared the calculation time of the moving average written in Python