Python Types

Introduction

Values

  • To get the type of a value use the type() function.
>>> name = 'tom'
>>> type(name)
<class 'str'>
>>> type(15)
<class 'int'>
>>> type(15.2)
<class 'float'>
>>> num = 20,300,2331,200
>>> type(num)
<class 'tuple'>

String Operation

  • Concatenation = combines strings together.
>>> first = '1st'
>>> second = '2nd'
>>> first + second
'1st2nd'
  • Repetition - repeats last string x times.
>>> (first+' ')*3
'1st 1st 1st '

Functions

  • Start with defining the function using the keyward def.
  • In interactive mode just enter to exit the function def.
def print_lyrics(repeat):
      print ("Im a lumberjack and i'm OK\n" * repeat)
      print("I'm sleep")

Modules

  • Example below shows importing the Math module.
  • Importing just PI.
  • Importing all constants.
import math
from math import pi
from math import *

Additional Resources

[* ]

Comments

rating: 0+x
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License