Introductory Workshop 2019

Introductory Workshop 2019

in

Introduction

Hello world!
Welcome to the Programming Club Introductory Workshop 2019!

Slack

First of all, please join our slack at https://pclubiitk.slack.com and join the channel #y19 to receive future notifications regarding events and other stuff from slack announcements directly.

Life@IITK

Programming Club worked extremely hard this summer to design your life @ IITK. We’ve brought together the various factes that will span your wonderful journey through 4 years. You’ll find your personal academic schedule, can subscribe to Gymkhana events, gloat over your PORs, feel hungry with the mess menus of all halls, not get lost with the Map and plan your year with a customizable calendar.

Terminal

A vital skill for any wanna-be programmer.

Try opening Terminal (sometimes known as Command Line) from the top-left menus. You should find it in the ‘System’ applications.

All the computer systems running today run over a terminal. You can do everything possible on a graphical system using this terminal (except of course the things requiring visual manipulation).

Some operating systems like Ubuntu might have a shortcut for this, you can open it using Ctrl+Alt+T, but not in the CC machines.

This is where you should learn to do all stuff related to programming, since this gives you unfathomable power.

Directory structure

Linux organizes files and folders in the following way:

  • Files are kept inside folders
  • You can enter and exit folders just like in Windows`
  • Folders are named in the following way: /path-to-folder/folder-name. This is what completely describes a folder (or a file).
  • We will be using the word directory for folders. Please note this.

To play around with this, try to use the following commands:

$ ls

# Lists contents of current folder
$ cd Desktop

# Move to the folder named Desktop
$ cd ..

# Go Back to previous folder
$ cd ~

# Go back to home folder(where you start on opening a terminal)
$ touch try.txt

# Create a new file named try.txt




Command Full form Description
ls list screen List all the files and folders in current directory
cd change directory Enter a folder whose path is known
pwd present working directory Full name of the current folder you are in
~ tilde Short form for your home folder
Ctrl+c(keyboard)   Exit/Cancel the current command
exit Exit Exit the terminal

VIM

We will be using vim to write programs. Open it from the applications menu. It’s very much like notepad from Windows.

$ vim try.txt

# Opens the file in vim

Type “An interesting programming activity”

If you are currently in insert or append mode, press ‘ESC’ key.

Press : (colon). The cursor should reappear at the lower left corner of the screen beside a colon prompt.

Type wq! and press Enter key to save and exit

$ cat try.txt

# Displays the content of the file.

Python Interpreter

Try running python command in the terminal. You should see something like:

Python 2.7.10 (default, Aug 12 2017, 22:05:31)
[GCC 4.9.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

The >>> is the place where you type. Try typing some of the commands like:

  • 2+2
  • print “hello world!”

Python

Here are a few python expressions:

  • a = 2+2
  • print "hello world!"
  • print a
  • print a*2

Try typing the above lines in vim and then saving the file as test.py.

After saving the file, you will need to navigate to its location from the terminal. Once you are in the folder containing test.py such that ls shows test.py as one of the files, run:

$ python test.py

The output would be something like this:

hello world!
4
8

You used an operator in a*2 - the multiplication operator. Other operators you need to try out are:

+ Addition
- Subtraction
* Multiplication
/ Division
** To the power of

If/Else

Now that you know how to write simple things, let’s proceed to something interesting.

Python reads spaces and tabs in your program as well. So for writing a complicated instruction for the computer, we will need to use multiple lines. We will indent the lines in such a way that the computer can understand that they are meant to be read together.

Try the following:

a = 0
if a is not 0:
    print "a is not zero"
else:
    print "a is zero"

That’s it! See?

Sometimes one condition isn’t enough though. In that case, you are allowed to chain up multiple conditions using and and or like so:

a = 0
b = 0
if a is 1 or b is a:
	print "Condition successful"
else:
	print "condition failed"

That’s all good, but what if you want multiple checks on a variable? For example, let’s say you want to do Action 1 if a<10 and Action 2 if a=10 and Action 3 when a>10, what then? This is where if-elif-elif-elif-...-else construct comes into the picture. Use it like this:

a = 4
if a < 0:
	print "a is less than 0"
elif a >= 0 and a < 4:
	print "a is between 0 and 4"
elif a >= 4 and a < 10:
	print "a is between 4 and 10"
else:
	print "a is bigger than 10"

Loops

Now for some loopy-loops!

a = 0
while a < 10:
    print a
    a = a + 1

Arrays

Now for some adorable arrays!

a=[]
for i in range(0,10):
	a[i]=int(raw_input)
print(a)

Functions

Adding modularity to your code

def max(a,b,c):
	if a/b>1:
		a,b=b,a
	if b/c>1:
		b,c=c,b
	if a/c>1:
		a,c=c,a
	print(a)
print(max(3,2,5))
print(max(0,0,0))

Reading input

You can read input from the user using something like this:

a = raw_input()
print "Hello mr. " + a

b = int(raw_input())
print b + 5

See what happens there?

CodeChef!

Try this link here

Can you solve this problem? Try writing a program for this! Please give it a try before you scroll down.

It’s quite small! Here’s the solution!

while True:
    x = int(raw_input())
    if x == 42:
        break
    print x

So you should now certainly create an account on CodeChef! Try submitting the solution there (don’t forget to select Python when you submit). Happy coding!

Follow up

Congrats on finishing up to here! If you’re now pumped up for trying out some more challenges, try your hand at the following problems:

Note C/C++/Java are commonly the preferred languages while participating in competitive contests. Recently, ACM ICPC has started allowing Python as well.

The ultimate feel to programming. P.S:-Attempt at your own risk.