Skip to main content

Module 1: Introduction To Python

https://learn.microsoft.com/en-us/training/modules/intro-to-python/

What is Python?


Python is an interpreted language - you need an interpreter. You can run code in 2 ways:

  • interactive - on the command line via the interpreter - >>> enter your command here
  • script - a full .py file that is executed.

Note: Most Python implementations partially compile scripts, turning the source code into byte code, which can run on any supported platform. This partial compile is done to improve performance for subsequent runs of the script and happens automatically. You can also generate a "compiled" version of the script and distribute an app without providing the full source code.

Python implementations


  • CPython
  • Anaconda
  • Iron Python
  • Jupyter Notebook

Using the REPL


"Read-Eval-Print-Loop", or REPL To use the REPL, simply type python into your command prompt.

>>> 1+1
2

Help


>>> help(object)
>>> help()

When there is more than one page, hit Enter to display line-by-line or space to go page by page.

image.png

Variables and basic data types in Python


Variables hold data in memory - They also hold types such as strings or integer. You can use operators such as + or - to manipulate the variables

x = 1         # assign variable x the value 1
y = x + 8 # assign variable y the value of x plus 8
z = y # assign variable z the value of y

Note: Variables are case sensitive and cannot start with a number

Working with numbers


x = 1       # integer
x = 1.0 # decimal (floating point)
x = 3.14159 # decimal (also tasty if cherry or strawberry)

Python creates integers from built-in data called int and decimals from float. The type() function returns a variable type.

x = 1
print(type(x)) # outputs: <class 'int'>

x = 1.0
print(type(x)) # outputs: <class 'float'>

x = 3.14159
print(type(x)) # outputs: <class 'float'>

The .0 at the end does impact how the computer handles the execution of this.

Booleans


True or False - capitalization is important. Booleans don't do any sort of mathematical operations, even though internally, true = 1 and false = 0

x = True
print(type(x)) # outputs: <class 'bool'>

Fun :)
image.png

Working with Strings


Strings are pretty common. They can contain names, places, things.

x = 'This is a string'
print(x) # outputs: This is a string
print(type(x)) # outputs: <class 'str'>
y = "This is also a string"

Concatenation


x = 'Hello' + ' ' + 'World!'
print(x) # outputs: Hello World!

y = "My name is" + ' ' + 'Dupo!'
print(y) # outputs: My name is Dupo!

What is print()


This is used to print things to the console - a Write-Host in PowerShell or a console.log() in JavaScript if you will.

print() is a function that you pass in something and it outputs to the console.

By default, print() outputs a newline character at the end, so subsequent calls start on a new line :)

Reading Keyboard Input


Programs are interactive - you create a program that runs, but input changes the direction of the program. Think about a video game and how the controller moves the character.

input() used with print():

name = input('Enter your name:')
print(name) # output: Dupo

## or without a parameter

print('What is your name?')
name = input()
print(name) # output: Dupo

Input with numbers


Input() treats numbers as strings.

x = input('Enter a number: ')
print(type(x)) # string
#fix:
x = int(input('Enter a number: '))
print(type(x)) # int

This leads us into the topic of error handling. What happens when you do a small program like this?

x = input('Enter a number: ')
y = input('Enter another number: ')
print(x+y)

Well, that's not right....or is it? Remember concatenation of strings from above :) image.png

Converting numbers into strings


The inverse can also be done.

x = 5
print('The number is ' + str(x))

image.png

Exercise 1: Building a Calculator


first_number = int(input('Type the first number: ')) ;\
second_number = int(input('Type the second number: ')) ;\
print("The sum is: ", first_number + second_number)

image.png

Summary


Official tutorial

Links: