Don't wanna be here? Send us removal request.
Text
Crash course in Python
The environment we use for running Python scripts is Jupyter a web based application that comes packaged with Anaconda.
To run Jupyter, open terminal and enter: jupyter notebook
Jupyter will open in a browser showing the file directory. Open a folder and in there click New> Python 3 [notebook]. Rename the notebook.
The notebook opens with a cell that effectively has a command line where we can enter Python code. Once the code is entered you run it by either clicking Run or pressing Shift+Enter.
Test example: (1+1) Run Out: 2
Arithmetic Operators
As in the test example other arithmetic operators can be used; +, -, *, /. Modulus operator, %. Outputs the remainder of a division. E.g.: (6%2), Output: 0 (7%2), Output: 1
Exponents, **. Raises to the power. E.g.: 6**2, Output: 36
Intiger division, //. Divides but rounds down to the nearest integer. E.g.: 13//6, Output: 2
Variables
Variables are declared using the = symbol. E.g. age = 56, declares age as a variable and gives it the initial value of 56.
Unlike other languages such as BASIC, Python allows multiple variables of different types to be declared in a single statement. E.g.:
age, name, gender = 25, "Jane", "female" age, gender, name Output: (25, 'female', 'Jane')
Print command
Running the above operations shows the output in an Output cell. However these are internal to Python. To display the results of an operation a print operation is required. E.g. print (name, age) Jane 30
Calculations with Variables
age = age + 5 using above declared variable. Output: 25 Can be simplified to: age += 5 or for subtraction, age -= 5
Numeric Data Types
Int = Integers, whole numbers e.g. 1, 4, 10. float = floating point decimals e.g. 2.33333333333 To determine which type a variable is use type e.g.: print (type (z)) <class 'float'> To convert a floating point variable to an integer use: int(7/3) Out: 2 A division operation always returns a float but you can also force a variable to be a float using, float() e.g. float(2) Out: 2.0
String Data Types
Strings are series of characters and are defined using either single or double quotes. E.g. name = '"John"' print (name) print (type (name)) "John" <class 'str'>
Use singles quotes if you need to use double quotes inside the string. dialogue = 'John said, "Hello Dave", and Dave replied "Hi John, you\'re amazing"' print (dialogue) John said, "Hello Dave", and Dave replied "Hi John, you're amazing"
Adding Strings
segment_one = 'I\'m 25' segment_two = 'years old' full_sentence = segment_one + " " +segment_two long_sentence = ((full_sentence + ', ')*10) print (long_sentence) print (len (long_sentence)) Output: I'm 25 years old, I'm 25 years old, I'm 25 years old, I'm 25 years old, I'm 25 years old, I'm 25 years old, I'm 25 years old, I'm 25 years old, I'm 25 years old, I'm 25 years old, 180
Boolean
comparison_operator = not 1 < 2 and 2 < 3 print (comparison_operator) Output: False
Methods
movie_title = 'Withnail and I' print (movie_title.count("i")) Output: 2
Data Types
0 notes