---
# System prepended metadata

title: 'Lesson23:Random List授業練習用'
tags: [Python]

---

# Lesson23:Random List授業練習用
###### tags: `Python`


## スライド練習１
:::info
code：
```python=
import random
num_list=[0,1,2,3,4]
print("getting number is",random.choice(num_list))
```
結果：
runfile('C:/Users/user/.spyder-py3/autosave/untitled31.py', wdir='C:/Users/user/.spyder-py3/autosave')
getting number is 4
:::

## スライド練習２
:::info
code：
```python=
import random
print(random.choice("python"))
```
結果：
runfile('C:/Users/user/.spyder-py3/autosave/untitled32.py', wdir='C:/Users/user/.spyder-py3/autosave')
t
:::

## スライド練習３
:::info
code：
```python=
import random
num_list=[0,1,2,3,4]
print(random.sample(num_list,3))
print(random.sample("python",3))    
string_list=["c+;","java","python","ca"]
print(random.sample(string_list,3))
```
結果：
runfile('C:/Users/user/.spyder-py3/autosave/untitled31.py', wdir='C:/Users/user/.spyder-py3/autosave')
[2, 1, 0]
['t', 'y', 'h']
['ca', 'python', 'java']
:::

## スライド練習４
:::info
code：
```python=
import random
num_list=[0,1,2,3,4,1,1,1]
print(random.sample(num_list,3))
print(random.sample("adgaadad",3))    
string_list=["c++","java","python","c#","python"]
print(random.sample(string_list,3))
```
結果：

runfile('C:/Users/user/.spyder-py3/autosave/untitled31.py', wdir='C:/Users/user/.spyder-py3/autosave')
[1, 3, 4]
['a', 'a', 'a']
['c#', 'python', 'java']
:::

## スライド練習５
:::info
code：
```python=
import random
num_list=[0,1,2,3,4,]
print(random.choices(num_list,k=3))
print(random.choices("python",k=3))    
string_list=["c++","java","python","c#",]
print(random.choices(string_list,k=3))
```
結果：
runfile('C:/Users/user/.spyder-py3/autosave/untitled33.py', wdir='C:/Users/user/.spyder-py3/autosave')
[3, 0, 1]
['o', 'n', 't']
['python', 'c++', 'java'] 
:::

## スライド練習6
:::info
code：
```python=
import random
num_list=[0,1,2,3,4,]
print("before",num_list)
random.shuffle(num_list)
print("after",num_list)
```
結果：

:::

## スライド練習7
:::info
code：
```python=
before [0, 1, 2, 3, 4]
after [1, 4, 0, 3, 2]
```
結果：

:::

## スライド練習8
:::info
code：
```python=
import random
num_list=[0,1,2,3,4,5,6,7,8]
print("before",num_list)
for i in range(5):
    ran_index1=random.randrange(9)
    ran_index2=random.randrange(9)
    temp=num_list[ran_index1]
    num_list[ran_index1]=num_list[ran_index2]
    num_list[ran_index2]=temp
print("after",num_list)
```
結果：
runfile('C:/Users/user/.spyder-py3/autosave/untitled32.py', wdir='C:/Users/user/.spyder-py3/autosave')
before [0, 1, 2, 3, 4, 5, 6, 7, 8]
after [0, 5, 2, 3, 4, 1, 6, 8, 7]
:::

## スライド練習9
:::info
code：
```python=

```
結果：

:::

## スライド練習10
:::info
code：
```python=

```
結果：

:::
