My First Python Code

In order to check if your development environment has been set up correctly, first run a "Hello World" script (and by 'script' in this case we mean one single line of code as you can see below).

By the way: save your scripts with an extension ".py". Your editor (in my case Visual Studio Code) will then recognize the language, format it properly, check your syntax, offer autocompletion and many other nice things.

By default, Python treats your code as if it was in a 'main()' procedure. Actually, if you run a script from the command line, Python gives the script the module name '__main__'. Much more about main() you can find at this site. But I prefer to learn by example, so here we go.

Learning by example

The second example illustrates some features of Python. The code in this example determines if a number is prime (that is, a number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11). By the way, the code below is not very efficient and could be improved in many ways.

By looking at this second example, you can learn a few things:

  • Python comes with a number of standard packages. Here we import the math package. Once imported, we can call the functions in that package.
  • The line 'v= 47' tells us something about variable typing; Python will try to create the variable v with the appropriate data type. In this case an integer.
  • The line 'print(('Is the number {} a prime number ? ').format(v), end ='')' is interesting and illustrates a few features of the print statement and how strings work. This could have also been written as 's = 'Is the number {} a prime number ?', followed by 'print(s.format(v), end = '')'. The string object has a number of properties and methods. For more details see the Python documentation
  • Python understands single and double quotes in strings. But they have to match. They can be embedded. Best practise: use single quotes where you can. 
  • The line 'm = math.ceil(math.sqrt(v))' shows the math package in action. First the square root is calculated (which would generally be a float), then the ceiling is determined. The ceiling is returned as an integer.
  • 'Range' returns an object with integers running from the start value up until, but not including, the end value. Hence the 'm + 1'.
  • The 'if' and 'for' constructs are simple and elegant. No brackets, just a colon at the end and indent the block that needs to be executed conditionally or in a loop.

My First Code Snippet

print("Hello, World!")

Is this number a prime?

# Check if f a number is a prime number - the dumb way 
  
import math

v = 47
  
# The second argument, end='', prevents the print command to create a new line 
print(('Is the number {} a prime number ? ').format(v), end ='')

# 1 is special with primes
if v > 1: 
      
   # Iterate from 2 to sqr(v), rounded up  
   m = math.ceil(math.sqrt(v))
   for i in range(2, m + 1): 
       # If num is divisible by any number between  
       # 2 and sqr(v)), it is not prime  
       if (v % i) == 0: 
           print('no') 
           break
   else: 
       print('yes') 
  
else: 
   print('no')

Previous chapter | Next chapter