0 ratings0% found this document useful (0 votes) 11 views7 pagesCoding
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Foundation Ace Booklet. Bakliwal Tutorials - IIT
Lecture 1 - Variables and I/O Operations
>] What Does Coding Mean Exactly?
We are all familiar with computers and have been using them to do all sorts of things. You
might be attending lectures, playing games and browsing the internet using your computer.
You must have noticed that in certain things, computers are very fast and efficient. Such
things include fast calculations, large data storage etc. On the other hand, there are a few
things where computers are not yet good at, Things like understanding the context, learning
things on their own etc. are rarely possible in computers.
For now, most computers are good at doing only those things that they are told to do. Also,
they need to be given very specific and precise instructions to make them do anything. They
lack the common sense and intelligence to understand vague or incomplete instructions.
So “Coding” is nothing but giving these specific and precise set of instructions to a computer
0 that it may do our bidding, Just like humans, computers can be taught to speak multiple
languages. Some commonly used languages to communicate with computers are C/C++,
Java, Python etc, While these are different languages and have different “syntax” (the way
and structure to write commands) the underlying logic to communicate through all these
languages remain the same.
We shall be using “Python” as our language of coding in this course. But our aim is to focus
more on the underlying logic of communication and not rote memorization of Python
Commands, In fact, at the end of every lecture, we shall include a list of all new commands
that you learnt with their usage, You will also be provided with such a list during the tests that
would be conducted as well. So do not focus on memorising the commands that we see
here. Instead, try to focus on the application part of those commands,
5] Why Python?
As we discussed, the choice of language is not an important aspect of this course as our
focus lies more on the algorithmic side. But to demonstrate that part, it is essential to work
with some language. We chose Python because it is one of the easiest languages to leam
and code in.
The syntax of Python is mostly like the english language with minimal use of brackets and
symbols. This makes it easy to read and understand the codes written in this language. To
write and run new codes, you do not need to download or install any new software. Basic
Python codes can run in some online code editors that can be opened from your web
browsers.
;BRANCHES AT: AUNDH | CAMP | FO ROAD | DECCAN] PAUD ROAD [PCNG | VIMAN NAGAR | WANOWRIE
PIMPLE SAUDAGAR | SATARA RD | HADAPSAR | SINHAGAD RD | SOLAPUR NASHIKFoundation Acc Booklet. Bakliwal Tutorials - IIT
>| Python IDE by Python Anywhere
IDE stands for Integrated Development Environment. This simply is a place where we write
our codes, execute them and see the resultant output. For our course, to demonstrate all our
codes, we shall be using a free online code editor by Python Anywhere. You may open the
editor yourself by visiting the link given below or by scanning the QR code. A login id would
be provided to each student to log into this site,
Link: [Link]
QR Code’
Glia
"
Ele
>| Variables
Variable Types and Values
Just like we assign @ name to certain values in Mathematics so that we may refer to those
values conveniently in multiple places, in coding as well we can have variables with some
values attached to them.
Consider the line below:
40
students
Now, this statement implies that you have created a new variable whose name is “student
and the current value of this variable is 40. Variable names should not contain any space in
them. To separate two words, an underscore character may be used. Thus apples, x,
al and no_of students are all valid variable names while no of students cannot
be a name of a variable.
Broadly, in this course, we shall encounter 2 main types of variables. Variables with numeric
values and variables with text values. In the above example, we have seen how to assign a
numeric value to a variable. To assign a text value to any variable, the value needs to be
enclosed in single or double quotes. Text values are also known as “strings” in coding.
;BRANCHES AT: AUNDH | CAMP | FO ROAD | DECCAN] PAUD ROAD [PCNG | VIMAN NAGAR | WANOWRIE
PIMPLE SAUDAGAR | [Link] | HADAPSAR | SINHAGAD RO | SOLAPUR [NASHIKFoundation Acc Booklet. Bakliwal Tutorials - IIT
Both the lines given below are valid ways to assign string values to variables:
myName = “Ameya”
yourName = ‘Vaibhav’
‘The backslash character (\) has a special significance in strings. It may be used to denote
‘some special characters when followed by certain letters. A few cases are given below.
‘\n’ + Denotes a new line
At — Denotes a tab (group of spaces)
‘AY’ — Denotes a backslash itself
Example:
print ("Name and Pin Code are...\nName: Ameya\tPin Code: 411038”)
print(*Backslash: \\"
Output:
Name and Pin Code are.
Pin Code: 411038
Operations on Variables
Almost all mathematical operations that you know can be done with numeric variables in
python. We shall be using the most common operations of addition (+), subtraction (-),
muttiplication (*) and division(/) for numeric variables.
For string variables, we can use the addition(+) operator as well. It simply combines the 2
strings into one string value.
Example:
sl = “Baklival”
wrutorials”
sl +" 482
Result:
> “Bakliwal Tutorials”
Note that you cannot add a string to a numeric variable directly. You need to convert them
into the same type before using the “+” operator. You may use the str() function to convert
numeric values to strings and int() or float() functions to convert the string values that look
like numbers to integers or real numbers respectively. Consider a few examples below.
Example:
sl = “My age ”
i-37
a= sltste
;BRANCHES AT: AUNDH | CAMP | FO ROAD | DECCAN] PAUD ROAD [PCNG | VIMAN NAGAR | WANOWRIE
PIMPLE SAUDAGAR | [Link] | HADAPSAR | SINHAGAD RO | SOLAPUR [NASHIKFoundation Acc Booklet. Bakliwal Tutorials - IIT
Result:
= “My age is: 37”
Example
muni = 11"
num2 = “12”
print (Without Conversion:”)
print (numl + aum2}
print (*\nWith Conversion:”)
print (int (numl) + int (num2))
Output:
Without Conversion:
m2
With Conversion:
2B
>| Taking User Input
We can use the input() function to take some inputs from the person who is running the
code. This function takes an input from the user when the code is run and gives that input
value as output to be used in the code, Note that the value given as output is in the form of a
string
Example:
s = input ()
Result:
‘ will be assigned whatever value the user inputs when the code is run.
When a string is given as an input to this function, that string is first displayed on the screen
and then user input is taken just like in the case of input()
Example:
5 = inp) ”
(“Enter your name:
Result:
“Enter your name:” will first be displayed on the sereen and then s will be assigned whatever value
the user inputs when the code is run.
;BRANCHES AT: AUNDH | CAMP | FO ROAD | DECCAN] PAUD ROAD [PCNG | VIMAN NAGAR | WANOWRIE
PIMPLE SAUDAGAR | [Link] | HADAPSAR | SINHAGAD RO | SOLAPUR [NASHIKFoundation Acc Booklet. Bakliwal Tutorials - IIT
>| Caesar Cipher
Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a
type of substitution cipher in which each letter in the plaintext is replaced by a letter some
fixed number of positions down the alphabet. For example, with a right shift of 2, D would be
replaced by F, E would become G, and so on. The method is named after Julius Caesar,
Who used it in his private correspondence,
In our lecture, we shall see how to write a code to encrypt a 4 lettered word using this cipher.
>| Lecture 1 Home Assignment
1. A list of all the codes that we discussed in lecture 1 will be shared with you. Go
through all those codes and play around with them by making small and meaningful
changes in them.
2. We have already seen how to encode a 4 letter word using Caesar Cipher. Write a
code on your own to decrypt an already encoded 4 letter word, (Assume coding was
done using the shift of 1)
3. Write a new code where you first take an input from the user for the amount of shift
desired in the Caesar Cipher and then encode and decode 4 lettered words
according to that shif.
>] New Functions Covered in this Lecture
print(x}
This function takes one text or number as input and displays it on the screen.
Example:
print(*B? means Blissful Times’)
Output:
BT means Blissful Times
;BRANCHES AT: AUNDH | CAMP | FO ROAD | DECCAN] PAUD ROAD [PCNG | VIMAN NAGAR | WANOWRIE
PIMPLE SAUDAGAR | [Link] | HADAPSAR | SINHAGAD RO | SOLAPUR [NASHIKBU
Foundation Acc Booklet. Bakliwal Tutorials - IIT
© str(num)
This function takes a number as input and converts it to the corresponding string value and
gives this string type variable as output
Example:
Result:
s>*S" and NOT s
t(str)
This function takes a number in string format as input and converts it to the corresponding
value in integer form and gives this integer type variable as output.
Example:
i= int (ss)
Result:
i= 5S and NOTi=*S".
© float(str)
This function takes a number in string format as input and converts it to the corresponding
value in real number form and gives this real number type variable as output.
Example:
pi = float ("3.14159")
Result:
pi= 3.14159 and NOT pi =°3.14159",
¢ord(char) and chr(num!
All the letter (character) values are stored as numbers in a computer. These functions help
us convert these character values to the numbers and number values to characters.
ord(char) function takes a character as an input and gives the corresponding number
associated with it as output. chr(num) function takes a number as an input and gives the
corresponding character associated with it as the output,
Example:
i = ord(“A”)
ch = chr(66)
;BRANCHES AT: AUNDH | CAMP | FO ROAD | DECCAN] PAUD ROAD [PCNG | VIMAN NAGAR | WANOWRIE
PIMPLE SAUDAGAR | [Link] | HADAPSAR | SINHAGAD RO | SOLAPUR [NASHIKFoundation Acc Booklet. Bakliwal Tutorials - IIT
Result:
i= 65
ch=“B
put()
This function takes an input from the user when the code is run and gives that input value as
output to be used in the code, Note that the value given as output is in the form of a string.
Example:
Result:
' will be assigned whatever value the user inputs when the code is run.
© input(str)
When a string is given as an input to this function, that string is first displayed on the screen
and then user input is taken just like in the case of input).
Example:
3 = input (“E:
inter your name:”)
Result:
“Enter your name:” will frst be displayed on the screen and then s will be assigned whatever value
the user inputs when the code is run.
;BRANCHES AT: AUNDH | CAMP | FO ROAD | DECCAN] PAUD ROAD [PCNG | VIMAN NAGAR | WANOWRIE
PIMPLE SAUDAGAR | [Link] | HADAPSAR | SINHAGAD RO | SOLAPUR [NASHIK
10