+ 1
How do i give two properties to one name?
Name: John Age:34 Sex: male A = {”John”: age:34, sex: male}?
10 odpowiedzi
+ 1
From your example:
Name: John
Age:34
Sex: male
A = {”John”: age:34, sex: male}?
Example of a class object:
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
Initialize an instance of the object:
A = Person("John", 34, "Male")
Use the variables within the object:
print(A.name)
print(A.age)
print(A.sex)
Output:
John
34
Male
+ 1
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
Creating multiple instances of Person:
A = Person("John", 34, "Male")
B = Person("Fred", 28, "Neutral")
C = Person("Billie", 35, "Female")
Create a list of Persons inside of personList:
personList = [A, B, C]
print("List of People (not Sorted)")
Gets attributes from each instance of object in the list:
for people in personList:
print(people.name, people.age, people.sex)
Output:
John 34 Male
Fred 28 Neutral
Billie 35 Female
0
Create a class object..
This reference may be a bit cryptic if you are new to python or OOP.
https://docs.python.org/3/tutorial/classes.html
0
William Owens indeed i am, lets reformulate what i asked: there is a list, lets call it x[],with 3 positions, i want to put john who is 34 and a male in the first position, so whenever i print(x[0]) i want, John, age:34, sex: male to be printed
0
Ok i need a method… not a list, but can i put his whole identity in a list? So when i print(x[0]) his whole information gets printed out
0
I think you are wanting to make a list of the objects:
https://www.kite.com/JUMP_LINK__&&__python__&&__JUMP_LINK/answers/how-to-create-a-list-of-objects-in-python
Is that what you are looking for? We can add attributes to the output.
0
William Owens yes exactly
0
Awesome let me know if you need any more assistance.
0
William Owens How do i add those attributes to the output?
0
Create dictionary.
mydict=
{'John': {'age': 34, 'sex': 'male'}}
output data: print(mydict['John']['age'] # output 34