0% found this document useful (0 votes)
23 views

Module 1.1

The document introduces Python basics like data types, operators, variables, and functions. It explains how to enter expressions, store values in variables, write programs, and dissect programs by covering print(), input(), len(), and type conversion functions.

Uploaded by

shreyu8pro2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Module 1.1

The document introduces Python basics like data types, operators, variables, and functions. It explains how to enter expressions, store values in variables, write programs, and dissect programs by covering print(), input(), len(), and type conversion functions.

Uploaded by

shreyu8pro2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to Python Programming

Module 1.1

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

Entering Expressions into the Interactive Shell

In Python, 2 + 2 is called an expression. Expressions consist of values (such as 2) and operators


(such as +), and they can always evaluate (that is, reduce) down to a single value. Here 2 + 2 is
evaluated down to a single value, 4.
A single value with no operators is also considered an expression, though it evaluates only to
itself, as shown here:

Math operators in Python (Highest to lowest 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).

National Institute of EngineeringPage 1


Introduction to Python Programming

The Integer, Floating-Point, and String Data Types

The most common data types in Python are listed in below.

 The integer (or int) data type indicates values that are whole numbers.
 Numbers with a decimal point, such as 3.14, are called floating-point numbers (or
floats).
 Always surround your string in single quote (') characters (as in 'Hello') so Python knows
where the string begins and ends. You can even have a string with no characters in it, '',
called a blank string.

String Concatenation and Replication


String Concatenation
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.

National Institute of EngineeringPage 2


Introduction to Python Programming

However, when + is used on two string values, it joins the strings as the string
concatenation operator.
Eg:

>>> 'Alice' + 42 It will return a syntax error

Replication

The * operator is used for multiplication when it operates on 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.
Eg:

The expression evaluates down to a single string value that repeats the original to a number
of times equal to the integer value.
>>> 'Alice' * 'Bob' It will return a syntax error
>>> 'Alice' * 5.0 It will return a syntax error

Storing Values in Variables

A variable is like a box in the computer’s memory where you can store a single value.

Assignment Statements

You’ll store values in variables with an assignment statement. 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.

National Institute of EngineeringPage 3


Introduction to Python Programming

1. A variable is initialized (or created) the first time a value is stored in it


>>> spam = 40
>>> spam
40
2. After that, you can use it in expressions with other variables and values
>>> eggs = 2
>>> spam + eggs
42
>>> spam + eggs + spam
82
3. When a variable is assigned a new value
>>> spam = spam + 2
>>> spam
42
The old value is forgotten, which is why spam evaluated to 42 instead of 40 at the end of
the example. This is called overwriting the variable.

Variable Names
You can name a variable anything as long as it obeys the following three rules:
1. It can be only one word.
2. It can use only letters, numbers, and the underscore (_) character.
3. It can’t begin with a number.

National Institute of EngineeringPage 4


Introduction to Python Programming

Variable names are case-sensitive, meaning that spam, SPAM, Spam, and sPaM are four
different variables.

Your First Program


# This program says hello and asks for my name.
print('Hello world!')
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?') # ask for their age
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')

Output
Hello world!
What is your name?
Al
It is good to meet you, Al
The length of your name is:
2
What is your age?
4
You will be 5 in a year.

National Institute of EngineeringPage 5


Introduction to Python Programming

Dissecting Your Program


Comments

The following line is called a comment.


# This program says hello and asks for my name.
Python ignores comments, and you can use them to write notes or remind yourself what the
code is trying to do. Any text for the rest of the line following a hash mark (#) is part of a
comment. Python also ignores the blank line after the comment. You can add as many blank
lines to your program as you want.

The print() function

The print() function displays the string value inside the parentheses on the screen.
print('Hello world!')
print('What is your name?') # ask for their name

When Python executes this line, you say that Python is calling the print() function and the
string value is being passed to the function. A value that is passed to a function call is an
argument.
Notice that the quotes are not printed to the screen. They just mark where the string begins
and ends; they are not part of the string value.
You can also use this function to put a blank line on the screen; just call print() with nothing
in between the parentheses.

The input() Function

The input() function waits for the user to type some text on the keyboard and press enter.
myName = input()
This function call evaluates to a string equal to the user’s text, and the previous line of code
assigns the myName variable to this string value.
You can think of the input() function call as an expression that evaluates to whatever string
the user typed in. If the user entered 'Al', then the expression would evaluate to myName =
'Al'.

National Institute of EngineeringPage 6


Introduction to Python Programming

Printing the User’s Name

The following call to print() actually contains the expression 'It is good to meet you, ' +
myName between the parentheses.
print('It is good to meet you, ' + myName)
Remember that expressions can always evaluate to a single value. If 'Al' is the value stored
in myName on the previous line, then this expression evaluates to 'It is good to meet you,
Al'. This single string value is then passed to print(), which prints it on the screen.

The len() Function

You can pass the len() function a string value (or a variable containing a string), and the
function evaluates to the integer value of the number of characters in that string.
print('The length of your name is:')
print(len(myName))
Just like those examples, len(myName) evaluates to an integer. It is then passed to print() to
be displayed on the screen. Notice that print() allows you to pass it either integer values or
string values.
>>> len('hello')
5
>>> len('My very energetic monster just scarfed nachos.')
46
>>> len('')
0

>>> 'I am ' + 29 + ' years old.'


Python gives an error because you can use the + operator only to add two integers together
or concatenate two strings. You can’t add an integer to a string because this is
ungrammatical in Python. You can fix this by using a string version of the integer.

National Institute of EngineeringPage 7


Introduction to Python Programming

The str(), int(), and float() Functions


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 it, as follows:

Because str(29) evaluates to '29', the expression 'I am ' + str(29) + ' years old.' evaluates to 'I
am ' + '29' + ' years old.', which in turn evaluates to 'I am 29 years old.'. This is the value that
is passed to the print() function.
The str(), int(), and float() functions will evaluate to the string, integer, and floating-point
forms of the value you pass, respectively.

 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.
o Eg:

National Institute of EngineeringPage 8


Introduction to Python Programming

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.

The int() function is also useful if you need to round a floating-point number down.

print('What is your age?') # ask for their age


myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')

The myAge variable contains the value returned from input(). Because the input() function
always returns a string (even if the user typed in a number), you can use the int(myAge)
code to return an integer value of the string in myAge. This integer value is then added to 1
in the expression int(myAge) + 1.
The result of this addition is passed to the str() function: str(int(myAge) + 1). The string
value returned is then concatenated with the strings 'You will be ' and ' in a year.' to evaluate
to one large string value. This large string is finally passed to print() to be displayed on the
screen.

National Institute of EngineeringPage 9


Introduction to Python Programming

National Institute of EngineeringPage 10

You might also like