1 - Intro To Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ENGR 423 – Drone Science Fundamentals

Intro to python 1

Printing data

The `print` function is a built-in command for python. it allows you to print text to the
screen, allowing you to display a text-based UI to the user or provide diagnostic
information.

As demonstrated above, the `print` function can take in a variety of inputs, allowing you
to print combinations of strings and number values. these values can be written literally
(“The diameter is”) or represented by named values called variables (radius, diameter).

Variable operations

Suppose you are writing a simple finance tracking program.

The account balance can be stored in a variable, then operations can be applied to
modify the balance.

1
© Pramod Abichandani, Ph.D., New Jersey Institute of Technology
ENGR 423 – Drone Science Fundamentals

Math module

In python, extra modules can be imported:

In the above example, the math module is only used for a single value, but it contains
many more commands:

2
© Pramod Abichandani, Ph.D., New Jersey Institute of Technology
ENGR 423 – Drone Science Fundamentals
ECET 411 – Embedded Systems II

For loops

In python, the _for loop_ is used to repeat, following the syntax below. he i variable is a
local variable ​used by the for loop. each time the for loop runs, i will be assigned to the
next value in the list. to demonstrate this, print i using the print() command on each
iteration of the loop:

Loops, or statements of any kind in python can be nested. Functions can be defined in
functions, loops can be part of an if-statement, etc.

3
© Pramod Abichandani, Ph.D., New Jersey Institute of Technology
ENGR 423 – Drone Science Fundamentals
ECET 411 – Embedded Systems II

if statements

Any time a comparison of values (a decision) is made, you can turn to an if statement! if
statements allow for equality or less/greater than checks, all of which can be negated.
these comparisons can be chained together, requiring that each condition pass or just
specific conditions.

The following comparison operators can be used:

● `==` equal to
● `!=` not equal to
● `<` - less than
● `>` - greater than
● `>=` greater than or equal to
● `<=` less than or equal to

When an if statement’s condition is not met, an optional `else` clause can be used to
accommodate the ‘failure’ case.

4
© Pramod Abichandani, Ph.D., New Jersey Institute of Technology
ENGR 423 – Drone Science Fundamentals

The `and` and `or` operators allow for compound condition checks.

The `elif` clause allows checking multiple conditions sequentially until one succeeds. the
`else` clause is used if none of the desired conditions are satisfied.

In addition to conditional operators, the values `True` and `False` can be used directly,
stored in a variable or returned by a function.

The following snipped defines a function that does nothing but return a single value,
`True`. The function can be called in-line with the if statement:

5
© Pramod Abichandani, Ph.D., New Jersey Institute of Technology

You might also like