1b: Basic syntax and data types
Table of Contents
🎉Table of contents with working links🎉
1. Homework check?
1.1. The panes of Spyder
- The editor pane (for "script mode", TP11)
- The variable explorer
- The console (for "interactive mode", TP11)
1.2. Saving!
Do you know where you are saving your script-name.py?
We recommend:
./PYW/1a ./PYW/1b ./PYW/1b/sphere.py
Etc.
2. Defining
2.1. Value
One of the basic units of data, like a number or string, that a program manipulates.
5 "hi" True
2.2. Variable
A name that refers to a value (or a memory location labelled with a name, and to which a value has been asigned).
var1 = 5 message = "hi"
2.3. Expressions
A combination of variables, operators, and values that represents a single result.
(2 + 2)
2.4. Statements
A section of code that represents a command or action. So far, the statements we have seen are assignments and print statements.
a = 3
2.5. Data types
The categories to which values belong to. In Python, we have four primitive Data Types:
- Integer
- Float
- String
- Boolean
a = 100 b = 100.0 c = "messag" d = True e = False
2.6. Operators
A special symbol that represents a simple computation like addition, multiplication, or string concatenation.
- - * / // % **
The operator //
is called the "floor division" operator in Python. It performs division and then rounds down to the nearest whole number.
It's closely related to the modulo operator %
, which shows the remainder of a division of two numbers.
print(8 // 3) print(16 % 3) print(2 ** 3)
2.7. Comments
Information in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program.
variable a will be used later on, in an if-statement a = True
2.8. Syntax error
“Syntax” refers to the structure of a program and the rules about that struc- ture. For example, parentheses have to come in matching pairs, so (1 + 2) is legal, but 8) is a syntax error.
(2+2)
2.9. Runtime error
The second type of error is a runtime error, so called because the error does not appear until after the program has started running.
Examples:
- ZeroDivisionError
- When a number is divided by zero.
- NameError
- When a name is used to which no value has yet been assigned
- TypeError
- When an operation is performed with incompatible types.
numerator = 10 denominator = 0 result = numerator / denominator print(result)
a = 2 print(a+b)
a = 2 b = "test" print(a+b)
3. Python conventions
PEP8 (Python Enhancement Proposal) link:
Variable names …
- can contain letters, digits and underscores
- cannot start with a digit
- lower case letters (in principle)
- underscores can be used to separate words in a name.
- should be informative/descriptive.
- should not use keywords or names of built-in functions
message_to_class = "please submit your answer: " print(messages20)
4. Type specific extras
4.1. Integers and floats
- PEMDAS
- Parentheses
- Exponentiation
- Multiply & divide
- Adding & subtracting
print((1 + 2) * 2)
When in doubt, use parentheses.
Also, have a look at this:
a = 5e5 # e5 = *10**5 print(a) print(a/2)
4.2. String
There are some special characters!
\n new line \t tab \" double quotation mark \' single quotation mark \\ backslash
print("hi all \\how are you doing?")
Concatenation
print("pigs" + " " + "cows")
Repetition
print("pigs " * 10)
4.3. Booleans
Boolean operators produce booleans. They work with boolean values, for example …
== means "is equal to"
print(True == False) print(True == True)
… but can also work with other data types:
print(1 == 2) print("hi" == "hi")
<= means bigger/smaller than, or equal to
print(2 <= 2)
!= means "not equal to"
print(2 != 3)
You can also combine things:
print(4 > 3 < 5)
5. Basic built-in functions in Python
5.1. print
print(<item1>, <item2>, . . . <itemx>, sep=' ', end='\n')
- Items separated from each other with ","
- sep = separator character (space by default)
- end = termination of character (new line by default)
#text = "this"+"is"+"a"+"test" print("this","is","a","test", sep='-', end='')
5.2. input function
variablename = input("here you write a sentence to be displayed to the user")
(Reminder to self: show in Spyder or console).
age = input("Please enter your age: ")
Note that the output is a string.
You can convert a string to other data types:
a = "30" a = int(a) print(a+1)
And you can convert an integer or a float to a string:
a = 30 a = str(a) print(a+"hi")
6. Preview of self-defined functions
The basic format for creating your own function in Python is:
def <function_name>(<formal parameters>): <command> ... <command>
The list of commands after the header is called a block
In Python not every function must return a value. If we want to return a value we must do so explicitly:
def adder(a,b): c = a+b return(c) print(adder(1,2))