快速生成20条Python数据行
发布时间:2023-12-11 09:10:13
1.
name = "John"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:
My name is John and I am 25 years old.
2.
x = 5
y = 7
print(f"The sum of {x} and {y} is {x + y}.")
Output:
The sum of 5 and 7 is 12.
3.
text = "Hello World"
print(f"The length of the text is {len(text)} characters.")
Output:
The length of the text is 11 characters.
4.
num_list = [1, 2, 3, 4, 5]
squared_list = [num**2 for num in num_list]
print(f"The squared list is {squared_list}.")
Output:
The squared list is [1, 4, 9, 16, 25].
5.
string_list = ["apple", "banana", "cherry"]
uppercase_list = [word.upper() for word in string_list]
print(f"The uppercase list is {uppercase_list}.")
Output:
The uppercase list is ['APPLE', 'BANANA', 'CHERRY'].
6.
import random
random_number = random.randint(1, 10)
print(f"The random number is {random_number}.")
Output (example):
The random number is 7.
7.
import datetime
current_date = datetime.date.today()
print(f"The current date is {current_date}.")
Output (example):
The current date is 2022-08-05.
8.
fruits = {"apple": 0.75, "banana": 0.50, "cherry": 1.25}
print(f"The price of an apple is {fruits['apple']} dollars.")
Output:
The price of an apple is 0.75 dollars.
9.
def add_numbers(a, b):
return a + b
result = add_numbers(3, 4)
print(f"The result of adding 3 and 4 is {result}.")
Output:
The result of adding 3 and 4 is 7.
10.
def is_even(number):
return number % 2 == 0
print(f"Is 6 an even number? {is_even(6)}.")
Output:
Is 6 an even number? True.
11.
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def calculate_area(self):
return self.length * self.width
rectangle = Rectangle(4, 5)
area = rectangle.calculate_area()
print(f"The area of the rectangle is {area}.")
Output:
The area of the rectangle is 20.
12.
import math
radius = 3
circumference = 2 * math.pi * radius
print(f"The circumference of a circle with radius {radius} is {circumference}.")
Output:
The circumference of a circle with radius 3 is 18.84955592153876.
13.
file_path = "data.txt"
with open(file_path, "r") as file:
lines = file.readlines()
print(f"The contents of the file are: {lines}.")
Output:
The contents of the file are: ['Line 1 ', 'Line 2 ', 'Line 3 '].
14.
import os
current_directory = os.getcwd()
print(f"The current directory is {current_directory}.")
Output (example):
The current directory is /Users/username/Documents.
15.
import requests
response = requests.get("https://api.example.com/data")
data = response.json()
print(f"The data received from the API is: {data}.")
Output:
The data received from the API is: {"name": "John", "age": 25}.
16.
import numpy as np
array = np.array([1, 2, 3, 4, 5])
mean = np.mean(array)
print(f"The mean of the array is {mean}.")
Output:
The mean of the array is 3.0.
17.
import pandas as pd
data = {'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(f"The dataframe is:
{df}.")
Output:
The dataframe is:
Name Age
0 John 25
1 Alice 30
2 Bob 35
18.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Line Plot')
plt.show()
Output:
This code will generate a line plot using the matplotlib library.
19.
import seaborn as sns
iris = sns.load_dataset("iris")
sns.pairplot(iris)
plt.show()
Output:
This code will generate a pair plot of the Iris dataset using the seaborn library.
20.
import tensorflow as tf
x = tf.constant(5)
y = tf.constant(3)
sum = tf.add(x, y)
with tf.Session() as sess:
result = sess.run(sum)
print(f"The sum of 5 and 3 is {result}.")
Output:
The sum of 5 and 3 is 8.
这些例子涵盖了Python中常见的数据操作和库的使用,包括字符串处理、数值计算、列表操作、文件读写、日期处理、网络请求、数据分析和可视化等方面。希望这些例子能够帮助你快速理解和应用Python编程。
