python built in functions code example

Example 1: python functions

def myFunction(say): #you can add variables to the function
  print(say)

myFunction("Hello")

age = input("How old are you?")

myFunction("You are {} years old!".format(age))

#this is what you get:

Hello
How old are you?
>>11 #lol my real age actually
You are 11 years old!

Example 2: modules in python

#use import statement to import modules
import random
r = random.randint(1,10)
#use from-import statements to import functions
from random import randint
r = randint(1,10)
#use from module import * statement to import all functions
from random import *
r = randint(1,10)

Example 3: functions in python

# Functions

""" All the functions are followed by the 'def' keyword in python"""

def Greet(User): # The arguments inside the brackets should be called or the function gives you an error
  print("Hello" + User + "!")
  
# Calling the function

Greet("Shivram")

# Output: 'Hello Shivram !'

Example 4: python3 any

# True since 1,3 and 4 (at least one) is true
l = [1, 3, 4, 0]
print(any(l))

# False since both are False
l = [0, False]
print(any(l))

# True since 5 is true
l = [0, False, 5]
print(any(l))

# False since iterable is empty
l = []
print(any(l))

Example 5: python functions list

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

Example 6: len python 3

len(list) # returns the length of a list, tuple, string, etc.