xxxxxxxxxx
#1
class Student:
def __init__(self, id=0, name=" ", major=" ", gpa=0.0):
self.id = id
self.name = name
self.major = major
self.gpa = gpa
def __repr__(self):
return f"Student: id= {self.id} , name = {self.name} , major = {self.major} , gpa = {self.gpa}"
#2
def get_student_with_max_gpa(path):
students = []
max_gpa_student = None
max_gpa = 0.0
with open(path, 'r') as file:
for line in file:
fields = line.strip().split()
id = int(fields[0])
name = fields[1]
major = fields[2]
gpa = float(fields[3])
student = Student(id, name, major, gpa)
students.append(student)
if gpa > max_gpa:
max_gpa = gpa
max_gpa_student = student
return students, max_gpa_student
#3
def split_and_store(s, path):
for student in s:
with open(f"D:\\Python\\{student.name}.txt", 'w') as file:
file.write(str(student.id) + " " + student.name + " " + student.major + " " + str(student.gpa))
#4
students, max_gpa_student = get_student_with_max_gpa("D:\\Python\\test.txt")
split_and_store(students, "D:\\Python")
xxxxxxxxxx
Y
.-^-.
/ \ .- ~ ~ -.
() () / _ _ `. _ _ _
\_ _/ / / \ \ . ~ _ _ ~ .
| | / / \ \ .' .~ ~-. `.
| | / / ) ) / / `.`.
\ \_ _/ / / / / / `'
\_ _ _.' / / ( (
/ / \ \
/ / \ \
/ / ) )
( ( / /
`. `. .' /
`. ~ - - - - ~ .'
~ . _ _ _ _ . ~
xxxxxxxxxx
Python is an interpreted, high-level,
general-purpose programming language.
//as you can also see to your right --------------------->
but also note interpreted, not compiled.
xxxxxxxxxx
Python is a High level and multi-purpose Programming language. It is commonly
used in data science ,AI(Artificial intelligence) and ML(Machine Learning).
If you are a beginner in programming it is mostly prefered because its
code is very simple and somehow similar to English Language.Even master level
people use python. Encourage you to learn it.
xxxxxxxxxx
Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
xxxxxxxxxx
'''Python is an interpreted, high-level and general-purpose programming
language.Python's design philosophy emphasizes code readability with its
notable use of significant whitespace. '''
#Examples
print("Hello World")
repeat = 5
for i in range(repeat):
print("This is the %i iteration" % i)
#2nd variation - print("This is the " + i + "iteration")
#3rd variation - print(f"This is the {i!r} iteration")
#4th variation - print(("This is the {} iteration).format(i))
import random #In order to use the random functions
class Person:
def __init__(self, name, age, height):
self.name = str(name)
self.age = int(age)
self.height = str(height)
person1 = Person("Name", random.randint(1, 100), str(random.randint(0, 7)) + "'" + str(random.randint(0, 11)) + '"')
print(f"Your name is {person1.name} and you are {person1.age} years old. You are also {person1.height}.")