Paiza Python Primer 1 Learn Programming

The basic grammar of Python 3 is completely free in Paiza, so I summarized it.

Paiza Python3 Primer

01 What is Python?

・ 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

python language reference

02 Let's write programming in Python

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.

  • (× Print、 ○ print)

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.

Exercise

1-4 "Let's correct the error part correctly" 5 "Let's output the specified character"

  1. Double quotation (") is full-width

lesson.py


print("Hello paiza learning")

print("hello paiza learning")

  1. The first double quote is full-width

lesson.py


print(”Hello paiza learning")

print("hello paiza learning")
  1. The second is single quotes

lesson.py


print("hello paiza learning')

print("hello paiza learning")

4.Print is capitalized

lesson.py



Print("hello paiza learning")

print("hello paiza learning")

  1. Let's write it ourselves

lesson.py


print("Hello, paiza learning")

03 Comments make the program easier to read!

・ 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")
'''

Exercise

  1. "Let's comment out" Comment out the first line. Make only the second line visible

lesson.py


#print("The brave was walking in the wilderness")
print("A monster has appeared")
  1. "Let's comment out" Let's output only the expected output value

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

04 Let's display HTML

・ 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="%")

Exercise

  1. "Let's output as HTML"

lesson.py


print("<p>The brave was walking in the wilderness</p>")
  1. Let's make the monster bold

lssson.py


print('''The brave was walking in the wilderness
<b>monster</b>Appeared''' )

05 Let's use variables

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

Exercise

  1. Exercise "Let's concatenate variable strings"

lesson.py


player = "Brave"
print(player+"Leveled up")
  1. Exercise "Let's correct mistakes"

lesson.py


player1 = "Brave"
print(player1 + "Recovered")
  1. Exercise "Let's correct mistakes Vol.2"

lesson.py


team = "Hero and warrior" 
print(team + "Recovered")
  1. Exercise "Output characters of variables"

lesson.py


player = "Brave"
print(player + "Was walking in the wilderness")
print(player + "Fought a monster")
print(player +"Defeated the monster")

06 Let's make dice

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

Exercise

  1. "Make 1 to 6 dice"

lesson.py


import random
number=random.randint(1,6)
print("The dice roll"+ str(number) + "is.")
  1. "Output damage to monsters" Random number range from 55 to 99

lesson.py


import random
number=random.randint(50,99)
print("To monsters"+ str(number) + "Damaged.")

07 Let's calculate with the operator

lesson.py


number = 100 + 20 
print(number +30)
print(number)

+Add
*Hang
/Divide
%remainder

Exercise

  1. Let's calculate

lesson.py


number = 1234*5678/2 
print(number)
  1. Let's calculate with variables

lesson.py


a = 31
b = 17
#Below, write the code that multiplies a and b and outputs the result.
print(a*b)
  1. Let's calculate the remainder

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)
  1. Let's calculate with parentheses

lesson.py


number=(1234+5678)*3
print(number)

Let's calculate the price

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

Exercise

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

09 Let's learn the data type

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