PYTHON BASICS
• Entering Expressions into the Interactive Shell
• The Integer, Floating-Point, and String Data Types
• String Concatenation and Replication
• Storing Values in Variables
• Your First Program
• Dissecting Your Program
PRACTICE QUESTIONS!!
Department of CSE, DSATM
ENTERING EXPRESSIONS INTO THE INTERACTIVE SHELL
The Python programming language has
a wide range of syntactical constructions,
standard library functions, and interactive
development environment features.
Let’s ignore most of that !!
You just need to learn enough to write some handy
little programs as quickly as possible!! 2
Department of CSE, DSATM
ENTERING EXPRESSIONS INTO THE INTERACTIVE SHELL
In Python, 2 + 2 is called an expression
A single value with no operators is also
considered an expression, though it evaluates only to itself
Department of CSE, DSATM
MATH OPERATORS IN PYTHON
math operators in Python
Department of CSE, DSATM
ORDER OF OPERATIONS ( PRECEDENCE)
• The ** operator is evaluated first.
• The *, /, //, and % operators are evaluated next, from left to right.
• The + and - operators are evaluated last , also from left to right.
• You can use parentheses to override the usual precedence if you need to.
• Whitespace in between the operators and values doesn’t matter for Python
(except for the indentation at the beginning of the line), but a single space is
convention.
So let’s Try these operators!!
5
Department of CSE, DSATM
ORDER OF OPERATIONS ( PRECEDENCE)
Department of CSE, DSATM
EVALUATION OF EXPRESSION
Department of CSE, DSATM
BAD PYTHON INSTRUCTIONS – SYNTAX ERROR MESSAGES
Department of CSE, DSATM
THE INTEGER, FLOATING-POINT AND STRING DATA TYPES
A data type is a category for values, and every value belongs to exactly one data type.
Department of CSE, DSATM
THE INTEGER, FLOATING-POINT TYPES
• The integer (or int) data type indicates values that are whole numbers.
Example: -2 and 30
• Floating point Numbers or floats are numbers with a decimal point.
Example : 3.14
• Note : even though the value 42 is an integer, the value 42.0 would be a
floating-point number.
10
Department of CSE, DSATM
STRING DATA TYPES
• Python programs can also have text values called strings, or strs.
• Always surround your string in single quote (') characters example :
'Hello' or 'Goodbye cruel world!‘
• Python knows where the string begins and ends because of ‘ ‘.
• You can even have a string with no characters in it '', called a blank string
or an empty string.
Note : error message Syntax Error: EOL while scanning string literal, you
probably forgot the final single quote character at the end of the string.
11
Department of CSE, DSATM
STRING CONCATENATION AND REPLICATION
The meaning of an operator may change based on
the data types of the values next to it.
For example, + is the addition operator when it
operates on two integers or floating-point values.
When + is used on two string values, it joins the
strings as the string concatenation operator.
12
Department of CSE, DSATM
STRING CONCATENATION AND REPLICATION
• The * operator multiplies two integer or floating-point values.
• But when the * operator is used on one string value and one integer value, it
becomes the string replication operator.
• Enter a string multiplied by a number into the interactive shell to see this in action.
So let’s Try these operators!!
13
Department of CSE, DSATM
STRING CONCATENATION AND REPLICATION
• The * operator multiplies two integer or floating-point values.
• But when the * operator is used on one string value and one integer value, it
becomes the string replication operator.
• Enter a string multiplied by a number into the interactive shell to see this in action.
So let’s Try this!!
14
Department of CSE, DSATM
STRING CONCATENATION AND REPLICATION
• The * operator can be used with only two numeric values (for multiplication), or
one string value and one integer value (for string replication). Otherwise, Python will
just display an error message, like the following:
So let’s Try this!!
Python wouldn’t understand
these expressions:
you can’t multiply two words,
and it’s hard to replicate an
arbitrary string a fractional
number of times.
15
STORING VALUES IN VARIABLES
A variable is like a box in the computer’s memory
where you can store a single value.
If you want to use the result of an evaluated
expression later in your program, you can save it
inside a variable.
An assignment statement consists of a variable
name, an equal sign (called the assignment
operator), and the value to be stored. If you enter
the assignment statement spam = 42, then a
variable named spam will have the integer value
42 stored in it. 16
Department of CSE, DSATM
STORING VALUES IN VARIABLES
17
Department of CSE, DSATM
STORING VALUES IN VARIABLES
18
Department of CSE, DSATM
VARIABLE NAMES
• A good variable name describes the data it contains.
• Python does have some naming restrictions.
• You can name a variable anything as long as it obeys the following three rules:
1. It can be only one word with no spaces.
2. It can use only letters, numbers, and the underscore (_) character.
3. It can’t begin with a number.
19
Department of CSE, DSATM
VARIABLE NAMES • Variable names are case-
sensitive, meaning that spam,
SPAM, Spam, and sPaM are
four different variables.
• Though Spam is a valid
variable you can use in a
program, it is a Python
convention to start your
variables with a lowercase
letter.
20
Department of CSE, DSATM
MY FIRST PROGRAM
So let’s Try this and see what will be the output ?? 21
Department of CSE, DSATM
MY FIRST PROGRAM
So let’s Try this and see what will be the output ?? 22
Department of CSE, DSATM
Dissecting Your Program
23
Department of CSE, DSATM
If you want to concatenate an integer such
as 29 with a string to pass to print(), you’ll
need to get the value '29', which is the
string form of 29. The str() function can be
passed an integer value and will evaluate
to a string value version of the integer, as
follows:
24
Department of CSE, DSATM
Try this and observe the output !
So let’s Try this
25
Department of CSE, DSATM
CALL THE STR(), INT(), AND FLOAT() FUNCTIONS AND PASS THEM
VALUES OF THE OTHER DATA TYPES TO OBTAIN A STRING,
INTEGER, OR FLOATING-POINT FORM OF THOSE VALUES.
The str() function is handy when you have an integer
or float that you want to concatenate to a string.
The int() function is also helpful if you have a number
as a string value that you want to use in some
mathematics.
For example, the input() function always returns a
string, even if the user enters a number.
Enter spam = input() into the interactive shell
and enter 101 when it waits for your text. 26
Department of CSE, DSATM
The value stored inside spam isn’t the integer 101 but
the string '101'.
If you want to do math using the value in spam, use the
int() function to get the integer form of spam and then
store this as the new value in spam.
Now you should be able to treat the spam variable as an
integer instead of a string.
So let’s Try this
27
Department of CSE, DSATM
NOTE THAT IF YOU PASS A VALUE TO INT() THAT IT CANNOT
EVALUATE AS AN INTEGER, PYTHON WILL DISPLAY AN ERROR
MESSAGE.
So let’s Try this 28
Department of CSE, DSATM
THE INT() FUNCTION IS ALSO USEFUL IF YOU NEED TO
ROUND A FLOATING-POINT NUMBER DOWN.
So let’s Try this
29
Department of CSE, DSATM
So let’s Try this 30
Department of CSE, DSATM
Practice Questions
31
Department of CSE, DSATM
PRACTICE QUESTIONS
32
Department of CSE, DSATM
THANK
YOU
Department of CSE, DSATM Acknowledgement : Al Sweigart