Skip to content
← Back to Community
How to the def command
Profile icon
Taiga123m

How do you do the def command that makes it repeat code and can make slight differences in python coding?

Voters
Profile icon
Taiga123m
Comments
hotnewtop
Profile icon
RYANTADIPARTHI

The def is basically a function. So it defines what you are writing. Like this:

def printhi(): return "hi" # in functions, good practice would be to use return printhi() # here you call it

Like that, functions are useful,instead of repeating code, you use the function name. you can learn more here:

https://www.w3schools.com/python/python_functions.asp
https://www.tutorialspoint.com/python/python_functions.htm

Profile icon
Coder100

A good example of this would be something like:

def greet(person): print("Hello %s!" % (person)) greet("Coder100") greet("Taiga") greet("someone else")

it will save you from a lot of typing!

Profile icon
Taiga123m

@Coder100
can you simplify it more or just explain a little bit because it's still a little confusing

Profile icon
Coder100
Profile icon
Taiga123m

@Coder100
nvm but what does person define? and does % mean +??? Also if I were to repeat the same exact code how do you make slight differences??

Profile icon
Taiga123m

This is my code but I forgot how to define the varible also what does import random do???

import random Afk2 = turtle.Turtle() Afk2.speed(18) def repeat(Afk2Code): print(Afk2) + (Afk2Code)) Afk2.color('white') Afk2.forward(168) Afk2.left(90) Afk2.pencolor('cyan') Afk2.begin_fill() Afk2.color('red') Afk2.circle(100,180) Afk2.end_fill() Afk2.left(90) Afk2.pendown() Afk2.forward(100)
Profile icon
TsunamiOrSumth

sorry about my last answer, i read the question wrong. could you specify?

Profile icon
Kookiez

@TsunamiOrSumth
get on the kookie repl plz

Profile icon
TsunamiOrSumth

@Kookiez
i gotchu

Profile icon
Taiga123m

@TsunamiOrSumth
its like a python def code that repeat code

Profile icon
TsunamiOrSumth

@Taiga123m
for loop?

Profile icon
Taiga123m

@TsunamiOrSumth
yea that yea

Profile icon
TsunamiOrSumth

@Taiga123m

for i in range(3): print('peepee')
Profile icon
Taiga123m

@TsunamiOrSumth
thanks but what about with turtle???

Profile icon
TsunamiOrSumth

@Taiga123m
should be same thing. just make a python repl and import turtle.

Profile icon
Taiga123m