ExampleGet your own Python Server Create a class named Person, use the __init__() function to assign values for name and age: class Person: def __init__(self, name, age): self.name = name self.age = agep1 = Per
Pythonint()Function ❮ Built-in Functions ExampleGet your own Python Server Convert the number 3.5 into an integer: x =int(3.5) Try it Yourself » Definition and Usage Theint()function converts the specified value into an integer number. ...
Pythonhasattr()Function ❮ Built-in Functions ExampleGet your own Python Server Check if the "Person" object has the "age" property: classPerson: name ="John" age =36 country ="Norway" x =hasattr(Person,'age') Try it Yourself » ...
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Pythonenumerate()Function ❮ Built-in Functions ExampleGet your own Python Server Convert a tuple into an enumerate object: x = ('apple','banana','cherry') y =enumerate(x) Try it Yourself » Definition and Usage Theenumerate()function takes a collection (e.g. a tuple) and returns it...
Pythonnext()Function ❮ Built-in Functions ExampleGet your own Python Server Create an iterator, and print the items one by one: mylist =iter(["apple","banana","cherry"]) x =next(mylist) print(x) x =next(mylist) print(x)
Pythonmap()Function ❮ Built-in Functions ExampleGet your own Python Server Calculate the length of each word in the tuple: defmyfunc(n): returnlen(n) x =map(myfunc, ('apple','banana','cherry')) Try it Yourself » Definition and Usage ...
Pythontype()Function ❮ Built-in Functions ExampleGet your own Python Server Return the type of these objects: a = ('apple','banana','cherry') b ="Hello World" c =33 x =type(a) y =type(b) z =type(c) Try it Yourself » ...
❮ Built-in Functions ExampleGet your own Python Server Compile text as code, and the execute it: x =compile('print(55)','test','eval') exec(x) Try it Yourself » Definition and Usage Thecompile()function returns the specified source as a code object, ready to be executed. ...
Pythonsorted()Function ❮ Built-in Functions ExampleGet your own Python Server Sort a tuple: a = ("b","g","a","d","f","c","h","e") x =sorted(a) print(x) Try it Yourself » Definition and Usage Thesorted()function returns a sorted list of the specified iterable object. ...