Codesphere Community
رفتن به کانال در Telegram
Welcome to the Codesphere Community Coding Using any device, all source code is here.... YouTube Channel: https://youtube.com/@Codesphere-D01010 Email: BusinessCodeSphere@gmail.com Admin/owner: @Alghifari_01
نمایش بیشتر1 744
مشترکین
اطلاعاتی وجود ندارد24 ساعت
-67 روز
-1930 روز
آرشیو پست ها
1 744
Hello everyone, sorry I didn't post code/tutorial today, tomorrow I will share the Game Source Code and Continue Learning Python Day 6👌
1 744
text = "Hello, World!"
new_text = text.upper().replace("WORLD", "Python".upper()).lower()
print(new_text)1 744
Days 5 🔥"English"
1.List
create a fruit list containing several fruits. Using the index, we can access and print the first element ('apple'). Then, we add a new element ('grape') to the list and print the total length of the list after addition.
#Defining and manipulating lists
fruits = ['apple', 'banana', 'orange']
print(fruits[0])
fruits.append('grape')
# Add elements to the list
print(len(fruits))
2.Tuple
A coordinates tuple contains two values that can be accessed via an index. With the technique of "unpacking," these values can be directly attributed to the variables x and y.
coordinates = (3, 4)
x, y = coordinates
# Unpacking tuples into variables x and y
print(x, y)
3.String Manipulation
uses built-in methods on Python strings to accomplish manipulations such as converting to uppercase and character replacement.
# String manipulation
greeting = "Hello, World!"
print(greeting.upper())
# Output: HELLO, WORLD! (change to uppercase)
print(greeting.replace('o', 'i'))
# Output: Hello, World! (replacing the character 'o' with 'i')
4.Modul
create a mymodule module which contains a simple greet function. Then, in another Python script, import the module and use the greet function to greet the name provided.
def greet(name):
return f"Hello, {name}!"
# Call the greet function and print the result
print(greet('Dzarel'))1 744
Days 5 🔥"Indonesia"
1.List
membuat list fruits yang berisi beberapa buah. Menggunakan indeks, kita dapat mengakses dan mencetak elemen pertama ('apple'). Kemudian, kita menambahkan elemen baru ('grape') ke dalam list dan mencetak panjang total list setelah penambahan.
# Mendefinisikan dan memanipulasi list
fruits = ['apple', 'banana', 'orange']
print(fruits[0])
fruits.append('grape')
# Menambahkan elemen ke dalam list
print(len(fruits))
2.Tuple
Tuple coordinates berisi dua nilai yang dapat diakses melalui indeks. Dengan teknik "unpacking," nilai-nilai ini dapat langsung diatribusikan ke variabel x dan y.
coordinates = (3, 4)
x, y = coordinates
# Unpacking tuple ke variabel x dan y
print(x, y)
3.String Manipulation
menggunakan metode bawaan pada string Python untuk menghasilkan manipulasi seperti mengonversi ke huruf besar dan penggantian karakter.
# Manipulasi string
greeting = "Hello, World!"
print(greeting.upper())
# Output: HELLO, WORLD! (mengubah ke huruf besar)
print(greeting.replace('o', 'i'))
# Output: Hello, World! (mengganti karakter 'o' dengan 'i')
4.Modul
membuat modul mymodule yang berisi fungsi sederhana greet. Kemudian, di skrip Python lain, mengimpor modul tersebut dan menggunakan fungsi greet untuk menyapa nama yang disediakan.
def greet(name):
return f"Hello, {name}!"
# Call the greet function and print the result
print(greet('Dzarel'))1 744
Days 4🔥 "English"
1.Use of Control Structures
# Use of Control Structures
def check_grade(score):
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'E'
return grade
# Use of Control Structures in Main Programs
if __name__ == "__main__":
student_score = 75
result = check_grade(student_score)
print(f"Mark {student_score} get grade {result}")
Explanation:
• The check_grade function accepts a value as a parameter and uses the if, elif, and else control structures to determine the grade based on a range of values.
• If the score is >= 90, the grade is 'A'; if >= 80, grade is 'B'; etc.
• The grade result is returned by the function.
2.Use of Loops
# Use of Loops
def print_even_numbers(n):
print("Even numbers up to", n, "is:")
for i in range(2, n+1, 2):
print(i, end=' ')
# Use of Loops in Main Programs
if __name__ == "__main__":
max_number = 10
print_even_numbers(max_number)
Explanation:
• The print_even_numbers function accepts a maximum value as a parameter and uses a for loop to print even numbers up to that value.
• range(2, n+1, 2) generates the range of even numbers from 2 to n (inclusive) with step 2.
• The main program uses this function to print even numbers up to a specified maximum value (here, 10).
3.Functions with Parameters and Return Statements
# Functions with Parameters and Return Statements
def calculate_rectangle_area(length, width):
area = length * width
return area
# Use of Functions in Main Programs
if __name__ == "__main__":
rectangle_length = 5
rectangle_width = 8
area_result = calculate_rectangle_area(rectangle_length, rectangle_width)
print(f"Area of rectangle with length {rectangle_length} and wide {rectangle_width} is {area_result}")
Explanation:
• The calculate_rectangle_area function accepts length and width as parameters, calculates the area of the rectangle, and returns the value of that area.
• In the main program, length and width values are given, and a function is called to calculate and return the area of the rectangle.
• The extensive results are then printed in informative sentences.
4.Use of Functions in Main Programs
# Functions with Parameters and Return Statements
def calculate_circle_area(radius):
area = 3.14 * radius**2
return area
# Use of Functions in Main Programs
if __name__ == "__main__":
circle_radius = 4
circle_area_result = calculate_circle_area(circle_radius)
print(f"Area of circle with radius {circle_radius} is {circle_area_result}")
Explanation:
• The calculate_circle_area function accepts the radius of a circle as a parameter, calculates the area of the circle, and returns the value of that nameIn thmainrogram (if __name__ == "__main__":), the circle radius value is determined, and the calculate_circle_area function is called to calculate the area of the circle.
• The extensive results are then printed in informative sentences.1 744
Days 4🔥 "Indonesia"
1.Penggunaan Struktur Kontrol
# Penggunaan Struktur Kontrol
def check_grade(score):
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'E'
return grade
# Penggunaan Struktur Kontrol dalam Program Utama
if __name__ == "__main__":
student_score = 75
result = check_grade(student_score)
print(f"Nilai {student_score} mendapatkan grade {result}")
Penjelasan:
• Fungsi check_grade menerima nilai sebagai parameter dan menggunakan struktur kontrol if, elif, dan else untuk menentukan grade berdasarkan kisaran nilai.
• Jika nilai >= 90, grade adalah 'A'; jika >= 80, grade adalah 'B'; dan seterusnya.
• Hasil grade dikembalikan oleh fungsi.
2.Penggunaan Loop
# Penggunaan Loop
def print_even_numbers(n):
print("Bilangan genap hingga", n, "adalah:")
for i in range(2, n+1, 2):
print(i, end=' ')
# Penggunaan Loop dalam Program Utama
if __name__ == "__main__":
max_number = 10
print_even_numbers(max_number)
Penjelasan:
• Fungsi print_even_numbers menerima nilai maksimum sebagai parameter dan menggunakan loop for untuk mencetak bilangan genap hingga nilai tersebut.
• range(2, n+1, 2) menghasilkan rentang bilangan genap dari 2 hingga n (inklusif) dengan langkah 2.
• Program utama menggunakan fungsi ini untuk mencetak bilangan genap hingga nilai maksimum yang ditentukan (di sini, 10).
3.Fungsi dengan Parameter dan Return Statement
# Fungsi dengan Parameter dan Return Statement
def calculate_rectangle_area(length, width):
area = length * width
return area
# Penggunaan Fungsi dalam Program Utama
if __name__ == "__main__":
rectangle_length = 5
rectangle_width = 8
area_result = calculate_rectangle_area(rectangle_length, rectangle_width)
print(f"Luas persegi panjang dengan panjang {rectangle_length} dan lebar {rectangle_width} adalah {area_result}")
Penjelasan:
• Fungsi calculate_rectangle_area menerima panjang dan lebar sebagai parameter, menghitung luas persegi panjang, dan mengembalikan nilai luas tersebut.
• Dalam program utama, nilai panjang dan lebar diberikan, dan fungsi dipanggil untuk menghitung dan mengembalikan luas persegi panjang.
• Hasil luas kemudian dicetak dalam kalimat informatif.
4.Penggunaan Fungsi dalam Program Utama
# Fungsi dengan Parameter dan Return Statement
def calculate_circle_area(radius):
area = 3.14 * radius**2
return area
# Penggunaan Fungsi dalam Program Utama
if __name__ == "__main__":
circle_radius = 4
circle_area_result = calculate_circle_area(circle_radius)
print(f"Luas lingkaran dengan jari-jari {circle_radius} adalah {circle_area_result}")
Penjelasan:
• Fungsi calculate_circle_area menerima jari-jari lingkaran sebagai parameter, menghitung luas lingkaran, dan mengembalikan nilai luas ternameDalammain utama (if __name__ == "__main__":), nilai jari-jari lingkaran ditentukan, dan fungsi calculate_circle_area dipanggil untuk menghitung luas lingkaran.
• Hasil luas kemudian dicetak dalam kalimat informatif.1 744
Quiz Python
Bagaimana cara membuat tuple yang berisi angka 1, 2, dan 3?
How to create a tuple containing the numbers 1, 2, and 3?
