From 54178b8fb02125e75b7e5f60262d4ddf08e51305 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Fri, 29 Dec 2023 16:36:13 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- .../input_output_string_int_float/README.md | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/python_basics/input_output_string_int_float/README.md b/python_basics/input_output_string_int_float/README.md index 98152d9..7caa8ef 100644 --- a/python_basics/input_output_string_int_float/README.md +++ b/python_basics/input_output_string_int_float/README.md @@ -13,6 +13,62 @@ Questions to [David Rotermund](mailto:davrot@uni-bremen.de) +## Examples + +### Input / Print + +```python +a = input() # <- test +print(a) # -> test +``` + +### String to int + +```python +a = int(5) +print(a) # -> 5 +print(type(a)) # -> +a = int(-5) +print(a) # -> -5 +print(type(a)) # -> +a = int("5") +print(a) # -> 5 +print(type(a)) # -> +a = int("-5") +print(a) # -> -5 +print(type(a)) # -> +a = int(" 5 ") +print(a) # -> 5 +print(type(a)) # -> +a = int("3.4") # ValueError: invalid literal for int() with base 10: '3.4' +a = int("Hello") # ValueError: invalid literal for int() with base 10: 'Hello' +``` + +### String to float + +```python +a = float(5) +print(a) # -> 5.0 +print(type(a)) # -> +a = float(5.1) +print(a) # -> 5.1 +print(type(a)) # -> +a = float(-5.1) +print(a) # -> -5.1 +print(type(a)) # -> +a = float("5.1") +print(a) # -> 5.1 +print(type(a)) # -> +a = float("-5.1") +print(a) # -> -5.1 +print(type(a)) # -> +a = float(" 5.1 ") +print(a) # -> 5.1 +print(type(a)) # -> +a = float("Hello") # ValueError: could not convert string to float: 'Hello' +``` + + ## [input](https://docs.python.org/3/library/functions.html#input) ```python @@ -90,3 +146,14 @@ floatvalue ::= [sign] (floatnumber | infinity | nan) > > If no argument is given, 0.0 is returned. +## [type](https://docs.python.org/3/library/functions.html#type) + +```python +class type(object) +class type(name, bases, dict, **kwds) +``` + +> With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.\_\_class\_\_. +> +> **The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.** +