+ 1

How do i give two properties to one name?

Name: John Age:34 Sex: male A = {”John”: age:34, sex: male}?

23rd Feb 2022, 9:30 PM
Lenoname
10 Answers
+ 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
23rd Feb 2022, 9:43 PM
William Owens
William Owens - avatar
+ 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
23rd Feb 2022, 11:07 PM
William Owens
William Owens - avatar
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
23rd Feb 2022, 9:37 PM
William Owens
William Owens - avatar
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
23rd Feb 2022, 9:51 PM
Lenoname
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
23rd Feb 2022, 9:57 PM
Lenoname
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.
23rd Feb 2022, 10:13 PM
William Owens
William Owens - avatar
0
William Owens yes exactly
23rd Feb 2022, 10:37 PM
Lenoname
0
Awesome let me know if you need any more assistance.
23rd Feb 2022, 10:38 PM
William Owens
William Owens - avatar
0
William Owens How do i add those attributes to the output?
23rd Feb 2022, 11:00 PM
Lenoname
0
Create dictionary. mydict= {'John': {'age': 34, 'sex': 'male'}} output data: print(mydict['John']['age'] # output 34
25th Feb 2022, 2:46 AM
Andrey