The basic grammar of Python 3 is completely free in Paiza, so I summarized it.
・ What is Python? Made for quick and effective system development General-purpose programming language
・ What can you do with Python? Full-scale Web service development Machine learning / AI, scientific calculation, statistical analysis
・ Adoption example Google, Youtube, Instagram, Dropbox, etc.
・ Features of python Easy to develop in scripting language There are libraries in various fields. -Machine learning-Big data analysis No matter who writes it, it will be written in the same way
Python case study , Case study
lssson.py
#Show hello world
print("hello")
Points that are easy to make mistakes </ strong> --Spaces are double-byte characters. --Double quotation marks are double-byte characters. --The function name is not written in all half-width lowercase letters.
What is a function </ strong> In Python, this "print" -like instruction is called a "function".
In programming, a function like "print" is used to instruct the computer to operate. When you write "print", Python understands that it prints characters, and it works.
Programming languages have many such functions. How each function works is determined by each programming language.
1-4 "Let's correct the error part correctly" 5 "Let's output the specified character"
lesson.py
print("Hello paiza learning")
print("hello paiza learning")
lesson.py
print(”Hello paiza learning")
print("hello paiza learning")
lesson.py
print("hello paiza learning')
print("hello paiza learning")
4.Print is capitalized
lesson.py
Print("hello paiza learning")
print("hello paiza learning")
lesson.py
print("Hello, paiza learning")
・ Write a comment using "#"!
・ Commenting out a program is called commenting out.
· Shift + 7 = single quote (')
-Comment out multiple lines by enclosing with 3 single quotes.
lssson.py
#comment(This line is ignored)
print("hello world")
#Commenting out a program is called "commenting out"
#print("hello world")
#You can comment out multiple lines by enclosing it with 3 single quotes.
'''
print("hello world")
print("hello world")
print("hello world")
'''
lesson.py
#print("The brave was walking in the wilderness")
print("A monster has appeared")
lesson.py
print("The brave was walking in the wilderness")
#print("A:A monster has appeared")
print("The brave got a great sword")
'''
print("A:A dragon appeared")
print("A:The Demon King appeared")
'''
print("The brave saved the world")
・ Display html
lesson.py
print("<h1>hello world</h1>")
print("<p><Everyone in the world")
print("<b>Hello</b></p>")
-Handle the print function on multiple lines You can handle multiple lines with 3 single quotes (').
lesson.py
print('''<h1>hello world</h1>
<p>Everyone in the world
<b>Hello</b></p>''')
・ If you do not want to start a new line with the print function Separate the strings with ",". In this case, the output strings are separated by spaces.
lessson.py
print("<h1>hello world</h1>", "<p>Everyone in the world", "<b>Hello</b></p>")
Another way is to add ", end =" "" inside the parentheses.
lesson.py
print("<h1>hello world</h1>", end="")
print("<p>Everyone in the world", end="")
print("<b>Hello</b></p>", end="")
・ If ", end ="% "" is specified, the characters are separated by "%".
lesson.py
print("<h1>hello world</h1>", end="%")
print("<p>Everyone in the world", end="%")
print("<b>Hello</b></p>", end="%")
lesson.py
print("<p>The brave was walking in the wilderness</p>")
lssson.py
print('''The brave was walking in the wilderness
<b>monster</b>Appeared''' )
lesson.py
player="Brave"
#Assigned data to a variable
print(player)
lesson.py
player="Warrior"
print(player + "Was walking in the wilderness")
print(player + "Fought a monster")
print(player + "Defeated the monster")
How to name variables </ strong> In Python, variables (local variables) are named according to the following rules.
・ First character: English character or "" (underscore) ・ Second and subsequent characters: English letters and numbers "" (underscore)
Variable name example: ○ player The first character is lowercase letters ○ weapon The first character is "" (underscore) ○ player01 Numbers after the second character ○ redDragon Uppercase letters after the second character
× 1player Numbers cannot be used in the first character × class Duplicate
In addition, function names used in Python, such as "print", are Since it is a reserved word, it cannot be used for variable names.
How to use variables in strings </ strong> Variables that store character data and character strings can be concatenated with the "+" symbol.
lesson.py
player = "Warrior"
print(player + "Was walking in the wilderness")
lesson.py
player = "Brave"
print(player+"Leveled up")
lesson.py
player1 = "Brave"
print(player1 + "Recovered")
lesson.py
team = "Hero and warrior"
print(team + "Recovered")
lesson.py
player = "Brave"
print(player + "Was walking in the wilderness")
print(player + "Fought a monster")
print(player +"Defeated the monster")
-Str (): A function that converts a numerical value into a character that represents a number. https://docs.python.org/ja/3/library/functions.html#func-str
lesson.py
number = 300
print("Slime" + number + "Appeared")
#If this is the case, it will not be displayed
print("Slime" + str(number)+"Appeared")
#Convert number to character data with str
Use random functions </ strong> -Random function: Returns a random number between 0 and 1.
・ Random module
https://docs.python.org/ja/3/library/random.html
lesson.py
import random
#Import random module
number=random.random()
print("Slime" + str(number) + "Appeared")
-Randint function: Returns a random integer value within the specified argument range
lesson.py
#I want to display numbers from 1 to 100 at random
import random
number=random.randint(1,100)
print("Slime" + str(number) + "Appeared")
#I want to display numbers from 10 to 20 at random
import random
number=random.randint(1,20)
print("Slime" + str(number) + "Appeared")
What is an argument </ strong> The argument of a function is the data given to the function. Arguments are written in parentheses following the function. If there are multiple arguments, separate them with "," (comma).
Argument example </ strong> print (data) data str (number) number
What is the return value </ strong> The return value (return) of a function is the data of the processing result of the function. Sometimes called a return value.
When you call the function random.randint (0, 10), a random number from 0 to 10 will be returned.
lesson.py
import random
number=random.randint(1,6)
print("The dice roll"+ str(number) + "is.")
lesson.py
import random
number=random.randint(50,99)
print("To monsters"+ str(number) + "Damaged.")
lesson.py
number = 100 + 20
print(number +30)
print(number)
+Add
*Hang
/Divide
%remainder
lesson.py
number = 1234*5678/2
print(number)
lesson.py
a = 31
b = 17
#Below, write the code that multiplies a and b and outputs the result.
print(a*b)
lesson.py
x = 8
y = 5
#Below, write the code that calculates the remainder when x is divided by y and outputs the result.
print(8%5)
lesson.py
number=(1234+5678)*3
print(number)
lesson.py
apple_price=350
apple_num=5
print("Apple unit price" + str(apple_price) + "Circle")
print("Number of apples to buy" + str(apple_num) + "Pieces")
total=apple_price*apple_num
print("total fee" + str(total) + "Circle")
#I want to set the number to 1-10, so import the random function
import random
apple_num=random.randint(1,10)
print("Number of apples to buy" + str(apple_num) + "Pieces")
total=apple_price * apple_num
print("total fee" + str(total) + "Circle")
#I want to set the unit price to 100-300
apple_price=random.randint(1,3)*100
print("Apple unit price" + str(apple_price) + "Circle")
total=apple_price * apple_num
print("total fee" + str(total) + "Circle")
I forgot to enclose it in str
lesson.py
# coding: utf-8
import random
number = random.randint(1, 10) #Number of animals 1-10
print("A slime weighing 100 kg" + str(number) + "Appeared")
#Total weight=Number of animals x 100
total=number*100
print("The total weight of slime"+str(total)+"Kilometer")
lesson.py
number = 100 #Numerical value
strings = "paiza" #String(Surround with double quotes)
print(number)
print(strings)
#Even when connecting characters+Can be used
#### **`lesson.py`**
```py
strings = "hello" + "paiza"
print(strings)
number = "100" + "30"
print(number)
# Change number variable to string with str function
print(str(number)+strings)
# Only the string type changed temporarily
print(number + 20)
##Exercise 1.Let's correct the error part correctly
lesson.py
x = 50
print(x - 10)
2.Let's correct the error part correctly 2
lesson.py
a = "Monster"
b = "appeared"
print(a + b)
3.Let's correct the error part correctly 3
Expected value 0123456789
lesson.py
a = "01234"
b = "56789"
print(a + b)
Recommended Posts