Tuesday 15 May 2018

Info Tech Seniors: May 16, 2018

For some of us Python is a little confusing.  But once you have a chance to play around with it, hopefully, you will understand it a little more.  The Code Academy Course is a bit difficult as it does not explain clearly -Why and How  to code in Python.  I have selected a few lessons and I want to you to try them in Skulptor.


You can run the examples and when you have finished clear the board and begin and then follow the lessons below.

What is Python?

Python is a popular programming language. It was created in 1991 by Guido van Rossum.
It is used for:
  • web development (server-side),
  • software development,
  • mathematics,
  • system scripting.

Python Syntax compared to other programming languages

  • Python was designed to for readability, and has some similarities to the English language with influence from mathematics.
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.
  • Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-bracket
Lesson starts here: Run these in Skulptor:

1. Python Indentations

Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important.
Python uses indentation to indicate a block of code.

Example:  Try this in Skulptor after you have cleared the editor.
Python will give you an error if you skip the indentation:

if 5 > 2:
  print("Five is greater than two!")

2. Creating Variable

a) Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

Example:
x = 5
y = "John"
print(x)
print(y)

 

b) Variables do not need to be declared with any particular type and can even change type after they have been set.
 [ the # shows us a message that the coder wants you to read but will not show up on the screen when you run the code]
Example:
x = 4 # x is of type int x = "Sally" # x is now of type str print(x)

3.Variable Names: Remember that variables are case-sensitive

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

4. Output Variables

The Python print statement is often used to output variables.
To combine both text and a variable, Python uses the + character:

 
Example:
x = "awesome"
print("Python is " + x)

Example:
x = "awesome"
y = "Python is " + x
print(y)
 

Example:
x = 5
y = 10
print(x + y)
 

Example: If you try to combine a string and a number, Python will give you an error: 
x = 5
y = "John"
print(x + y)
 


When you have completed these steps: Go back to Code academy and see if it is a little more clear : We will try some more tomorrow.
 

No comments:

Post a Comment