[Beginner] Python functions

Try using a function in Python

Specify the default argument
def hello(name="A", age=10):
	result = "Hello I'm " + name + ", I'm " + str(age)
	print(result)

>>> hello()
Hello I'm A, I'm 10
>>> hello("Bob", 20)
Hello I'm Bob, I'm 20
Reverse string arguments
>>> def reverse(s):
	cnt = len(s)
	result=""

	if cnt < 1:
		result = s
	else:		
		i = cnt-1
		while i > -1:
			result = result + s[i]
			i = i - 1
	print(result)

	
>>> reverse("abcde")
edcba
>>> reverse("a")
a
Is it divisible by 3, divisible by 5, or divisible by both?
>>> def fb(n):
	mod3 = n % 3
	mod5 = n % 5
	result = ""
	if mod3 == 0:
		result += "Fizz"
	if mod5 == 0:
		result += "Buzz"
	return result

>>> i = 1
>>> while i <= 20:
	print(i, fb(i))
	i = i + 1

1 
2 
3 Fizz
4 
5 Buzz
6 Fizz
7 
8 
9 Fizz
10 Buzz
11 
12 Fizz
13 
14 
15 FizzBuzz
16 
17 
18 Fizz
19 
20 Buzz

Recommended Posts

[Beginner] Python functions
Python functions
Beginner ABC154 (Python)
Beginner ABC156 (Python)
python beginner memo (9.2-10)
python beginner memo (9.1)
Python beginner notes
[Beginner] Python array
Beginner ABC155 (Python)
#Python basics (functions)
Beginner ABC157 (Python)
PyQ ~ Python Beginner ~
Python Easy-to-use functions
Python basics: functions
Python beginner memo (2)
Python beginner Zundokokiyoshi
Python Beginner's Guide (Functions)
Python basic course (12 functions)
[Python] Memo about functions
# 4 [python] Basics of functions
Python built-in functions ~ Zip ~
Wrap Python built-in functions
Curry arbitrary functions with Python ....
Python> lambda> tiny functions / callback functions
Getting Started with Python Functions
Python3 programming functions personal summary
python super beginner tries scraping
Python
Python beginner launches Discord Bot
Study from Python Hour3: Functions
Overriding library functions in Python
Keyword arguments for Python functions
Python for super beginners Python #functions 1
Paiza Python Primer 7: Understanding Functions
Python 3 sorted and comparison functions
Web scraping beginner with python
[Python beginner] Update pip itself
Python functions learned in chemoinformatics
Atcoder Beginner Contest 152 Kiroku (python)
Python higher-order functions and comprehensions
Python control syntax, functions (Python learning memo ②)
Azure Functions: Try Durable Functions for Python
[python] Manage functions in a list
About python dict and sorted functions
"Effective Python 2nd Edition" Chapter 3 <Functions>
Let's play with Excel with Python [Beginner]
[Python beginner] Divide one list (5 lines).
Using global variables in python functions
10 functions of "language with battery" python
[Beginner] Extract character strings with Python
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
Dynamically define functions (methods) in Python
AtCoder Beginner Contest 174 C Problem (Python)
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
kafka python
Python3> Functions> Function Renaming Mechanism> f = fib
[Python3] Dynamically define global variables in functions
Python basics ⑤
A note where a Python beginner got stuck
python + lottery 6
Python Summary