INTRODUCTION TO PYTHON
ROBOTICS CLUB @ NIE
DAY - 1
INTRODUCTION
Python is a high-level object oriented programming language used in various
applications like web development, connecting database systems, in complex
mathematical operations, handling big data and also in rapid prototyping.
It has a simple syntax which is similar to the English language and allows developers
to write programs with fewer lines when compared to other programming languages
It is a free and open source, cross platform language which means it can run on
multiple platforms like Windows, Linux, macOS.
COMPARISON
PYTHON C++/C
Programs are easier to write because of the Difficult in comparison due to its complex
simple syntax syntax
Has higher readability because syntax is closer Readability is less and the codes are more
to the English language complex due to the various predefined
Here variables are accessible even outside the structures and syntaxes
loop The scope of the program is confined within the
Rapid prototyping is possible due to small size loop
of the code Here code sizes are longer hence rapid
prototyping is not possible
VALUES AND DATA TYPES
A VALUE is one of the basic things a program
works with, like a letter or a number.
In programming, DATA TYPE is an important
concept.
Variables can store data of different types, and
different types can do different things.
You can find the data type using the syntax:
print(type(xyz))
Example:
print(type('hello friends'))
VARIABLES
A variable is nothing but a name that refers to a value.
Python does not have a specific command to declare a variable. There is no need to declare the variables
with a particular type.
The data type of the variable can be obtained using the type() function.
All variable names are case sensitive.
Rules for writing variable names
• A variable name must start with a letter or the underscore character.
• A variable name cannot start with a number.
• A variable name can only contain alpha-numeric characters and underscores.
KEYWORDS
Keywords are the reserved words in python
They are case sensitive and cannot be used as variable or function names. They are used to define the
syntax and structure of the python language
IDENTIFIERS
Entities like class, function, variables etc are termed as identifiers. These are used to differentiate one
entity from another.
Rules for writing identifiers
Identifiers can be a combination of letters( both uppercase and lowercase), digits or an underscore.
An identifier cannot start with a digit
Keywords cannot be used as identifiers
We cannot use special characters like @ $ % ! in our identifiers
An identifier can be of any length
STATEMENTS AND INDENTATIONS
A STATEMENT is a unit of code that the Python interpreter can execute. We have seen two kinds of
statements: expression statement and assignment.
Example:
message = “Stay safe ” # is an assignment statement
print(message) # is an expression statement
The assignment statement produces no output.
The body of an function, loop etc always starts with an INDENTATION. The amount of indentation can
be to our desire but it must be consistent through the code.
OPERATORS
Operators are special symbols that designate that some sort of computation should be performed. The
values the operator is applied to are called as operands.
The operators +, -, *, /, and ** perform addition, subtraction, multiplication, division, and
exponentiation.
The + operator works with strings, but it is not addition in the mathematical sense. Instead it performs
concatenation, which means joining the strings by linking them end to end.
Example:
a , b = “Stay ” , “safe”
c=a+b
print(c)
COMMENTS
As programs get bigger and more complicated, they get more difficult to read. For this reason, it is a good idea to add
notes to your programs to explain in natural language what the program is doing. These notes are called comments, and
in Python they start with the # symbol.
Comments can be placed at the end of a line, and Python will ignore the rest of the line.
Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in
your code and place your comment inside it.
Example:
"""
This is a comment written in
more than one line
"""
print("Hello, World!") #This is a comment and interpreter ignores this.
CONDITIONAL EXECUTION
BOOLEAN EXPRESSION
A boolean expression is an expression that is either true or false
The operators used for comparison are:
== , != , < , > , >= , <= , is , is not.
Example:
print(10 > 9)
print(10 == 9)
print(10 < 9)
LOGICAL OPERATORS
There are three logical operators: and, or, and not.
IF STATEMENT
Here there are only one possibility and the condition determines whether to execute the body statement
or not.
Syntax:
if boolean_expression :
body_statements
The boolean expression after the “if statement” is called the condition. We end the “if statement” with a
colon character (:) and the line(s) after the if statement are indented.
Based on the evaluation of the condition, the sequence of statements are executed.
Example:
input(x) # takes value from the user
if x > 0 : #checks the condition
print('x is positive') #if condition is true give the required output
Yes
x>0
print('x is positive')
IF ELSE STATEMENT
Here there are two possibilities and the condition determines which one gets executed.
Syntax:
if boolean_expression :
body_statements
else :
body_statements
The condition must either be true or false, exactly one of the alternatives will be executed. The
alternatives are called branches, because they are branches in the flow of execution.
Example:
input(x)
if x > 0 :
print(‘x is positive’)
else :
print(‘x is negative’)
Yes No
x>0
print(‘x is positive’) print(‘x is negative’)
ELSE IF STATEMENT
Here there are more than two possibilities.
Syntax:
if boolean_expression :
body_statements
elif boolean_expression :
body_statements
elif boolean_expression :
body_statements
Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true,
the corresponding branch executes, and the statement ends. Even if more than one condition is true, only
the first true branch executes.
Example:
input(x)
if x > 0 :
Yes
print(‘x is positive’) x>0
elif x < 0 : print(‘x is positive’)
print(‘x is negative’)
else :
print(‘x is 0’)
Yes
x<0 print(‘x is negative’)
print(‘x is 0’)
NESTED CONDITIONALS
Here one condition can be nested into another condition.
Example:
if x == y :
print('x and y are equal')
else:
if x < y :
print('x is less than y')
else:
print('x is greater than y’)
The outer conditional contains two branches. The first branch contains a simple statement. The second
branch contains another if statement, which has two branches of its own. Those two branches are both
simple statements, although they could have been conditional statements as well.
Yes No
x == y
No
Yes
x<y
print('x and y are equal')
print('x is less than y') print('x is greater than y’)
LOOP
WHILE LOOP
A while loop is a programming concept that, when it's implemented, it executes a piece of code over
and over again while a given condition still holds true.
Syntax:
while test_expression :
Body of while
In the while loop, test expression is checked first. The body of the loop is entered only if
the test_expression evaluates to True. After one iteration, the test expression is checked again. This
process continues until the test_expression evaluates to False.
Example:
number = 2
# Condition of the while loop
while number < 5 :
print("Thank you")
# Increment the value of the variable "number by 1"
number = number + 1
FOR LOOP
A for loop is a programming concept that, when it's implemented, it executes a piece of code over and
over again "for" a certain number of times, based on a sequence.
Syntax:
for iterator_var in sequence :
statements(s)
Example:
n=4
for i in range(0, n):
print(i)
NESTED LOOP
A loop inside another loop is called nested loops.
Nested while loop syntax:
while expression :
while expression :
statement(s)
statement(s)
Nested for loop syntax:
for iterator_var in sequence :
for iterator_var in sequence :
statements(s)
statements(s)
Example:
for i in range(1, 5) :
for j in range(i) :
print(i , end=’ ‘)
print()
THANK YOU