Python
Chapter summary - Unit 1
- Unit 1
Python Fundamentals:
An Introduction to Python programming: Introduction to Python, IDLE to develop programs;
How to write your first programs: Basic coding skills, data types and variables, numeric data, string data, five of the Python functions;
Control statements: Boolean expressions, selection structure, iteration structure;
Define and use Functions and Modules: define and use functions, more skills for defining and using functions and modules, create and use modules, standard modules
Contents
- What is python?
Python is a high level interpreted language that is preffered in rapid development of programs due to it’s easy and simple syntax. - IDLE
IDLE is the short form of integrated development learning environment. - Data types in python
int(5), string(’this is a string’ or this is a string), tuple( (5,3,5) ), float(5.3), bool(true/false) … - Python interation structure
Unlike other languages python uses indententation instead of using brackets.
Comments
Comments are a piece of code that is essentially “dead code” these are essential to show the programmer what the code does and not to do anything.
Comments in python -
| |
Basic IO in python
- Printing a statement \
| |
Putting another variable in the output message
| |
- Concatenation Concatenation is the process of joining two or more strings. Concatenation can be done in python by the following way.
Here we have added a space to seperate the hello and the world.
- Getting input by a python statement
- fstrings
fstrings make it easier for the user to shove in multiple variables in the same print statement.
Function chaining, taking numerical inputs and type casting
Inputs in python can be easily be obtained from the input command, however this command only provides the data to the interpretor as a string to the variable. To change the datatype for the variable we have to use explicit type casting.
This will give the output as an integer since we have used explicit type conversion. \
Note
Implicit type conversion is when the program automatically changes the datatype of a variable to suit the needs of the further process. Explicit type conversion is when the programmer has to manually change the datatype of a value for the change of the datatype.
Loops
Python has mainly two loops, these are for and while loops
For loop is a collection based loop, that is it works by operating on a set where as while works as a condition based loop where the loop operates on a condition.
Syntax of for loops \
Python interpretor will prioritise the arguments in the …range() function in this way ending_num > starting_num > step. So using an Syntax of while loops \
Examples of the loops - printing numbers from 1 to 9
Multiline statements in python
Since python uses indentation for its language instaed of having code inside of brackets it is not natural to write multiline statements inside python. Usage of the “\” symbol signals the interpretor that its a multiline statement.
Logical statements
There are a few logical statements in python this includes if, else, elif, and, or.
Example
There are a few logical statements in python this includes if, else, elif, and, or.
The usage of basic if-else statements
Example
The usage of compound if-else statement with and/or
| |
Switch statements
Switch statements are the extension of if-else ladder and built for shotening the syntax. However this feature is only supported on python versions above 3.10.
Syntax
- Option can be replaced with the veariable you want the switch to run
- Case 1, 2, 3 can be replaced the the value of variable you want to be and it also supports string values
- The
case _gives the default value and executes if none of the cases match
Functions
Functions is a set of code that can be called by the user at anytime based on the need. A function is reusable any number of times.
Syntax of a function
Modules
Modules or packages are a way to package your code into a collective for another program. There are a lot of modules in python such as numpy and math.
Example
Creating your own package
- In a python file make functions
- Save the file in which your other program is present
- Import the module into your own file.
Link
Default arguments
Default arguments are the what the funcion seeks to when the user provides no arguments when a function is called.
Default arguments must always be given from right to left
Global variables
There are two types of variables in python, local and global variables. Local variables are only defined within a function block and global variables are defined within the whole program.
Typically global variables are frowned upon, since they posess potential security vurnerablities.
Multiple variable assignments
In python multiple variable values can be assigned in the same line, this can be extensively used if you use a swapping function.
This will swap the values