Basic coding in Python
[email protected]
Python
• an interpreted language (no compilation is necessary, directly executes instructions)
• is extensible to other languages (eg C)
• is a high-level language (eg. more intuitive, uses natural language elements)
• can be split into modules to reuse in other Python programes
• is (much) shorter to write than eg. C or Java
Python
1.Hello World
2.Strings
Python REPL
3.Functions
Read
4.Booleans Evaluate
Print
5.Loops Loop: get ready for the next input
Python: Data Types •stringwith
•string = anything = anything
" aroundwith
it is "aaround
string it is a string
1. strings = some text with no numerical value. e.g. "Hello World“: > anything with a “ sign is a string
2. int = A whole number. e.g. 3: > any integer number without “ sign
3 float = A decimal value e.g. 3.1415: > any decimal number without “ sign
4. bool = A Boolean value e.g. True or False: > Classifications without “ sign
Lexicon:
Int – integer
Bool – George Boole
Python: Data Types
1. strings = some text with no numerical value. e.g. "Hello World"
2. int = A whole number. e.g. 3
3 float = A decimal value e.g. 3.1415
4. bool = A Boolean value e.g. True or False
Strings
• A string in Python is a sequence of characters (text)
• A Python string needs quotes around it for it to be recognized: 'Hello, World‘
• Because of the quotes, Python understands this is a sequence of characters and not a command,
number, or variable
• Can use single or double quotes (but not 2 single quotes) around each letter
• English wording like ‘It’s‘ (it is) will cause syntax error sue to multiple single quotes in a string
• To get around, use backward slash: 'It↘’s’
Python : Hello, world / Input = name
Concatenation
1. Concatenation = to add strings
2. Place a + between strings
Hit
RUN
Concatenation :
Be mindful of the space (after hitting Space key) between asterisks and strings
Hit
RUN
Concatenation
• Concatenations (using +) a string with any other data type will give an error
• Turn the other data type into a string using [str] command (from string)
Hit
RUN
Insert your age
Boolean operators and conditional operators
Operator Meaning Hit
> greater than RUN
< smaller than
>= greater than or equal to
<= smaller than or equal to
== is equal
!= is not equal
Also work with strings of letters:
• Uppercase letters are ‘smaller’ than lowercase letters, eg: ‘M’ < ‘m’
• Digits are smaller than letters: ‘1’ < ‘a’