Python in 4 sittings: Sitting 1

Introduction to Python

Summary of the first class

13th June, 2021

© pi4py.netlify.app

feel free to contact us;

arabindo@protonmail.com

kaustavbasu97@gmail.com

Basic Data Types

As usual we have integers, floats and strings. Here, in Python you really don't need to worry about defining the data types explicitly. You can just keep using any particular variable with any data types at different instances of the program. Python interpreter will figure it out for out (lazyness is awesome! xD).

You can perform comparison operations with different data types (exception: strings). Example:

Although the result of the comparision operations in the first two lines have been evaluated as true, if you check their data types, they'll be different. For that you just need to type type(variable) or type(data)

`# This is formatted as code`

Now, here comes the print statement -

General syntax : print("statements", variable1, variable2,...)

Now let's move to the Control statements: Generic good old IF-ELSE

Let's say we have two numbers a and b, and we want to perform comparisions between them.

For taking input for a and b from the user, The input() function can be used. You can also add a prompt to it (although it's not mandatory), by input("statements") ** Remember, whenever you try store an user-input it gets stored as a string. Therefore, you must convert those inputs into number(or any other specific type). In the next example we have converted an input string to float. You can verify the input data type by using the type(variable) command, as we stated earlier, in line In[2]

Let's make it a bit prettier:

notice those are float point now, can you figure it out why so?

General Syntax:

if(logical expressions):
    instructions
    ...
    ...
elif(logical expressions): #elif and else blocks are optional
    instructions
    ...
    ...
else:
    instructions
    ...
    ...

indentations are important. While writing code you must focus on the indentation!

In other languages like C or C++ or Java, indentation is an optional thing, i.e. even without indentations, the codes work properly. Do you know the reason? Well, we can use braces there to segmentify/group different regions of the script.

The purpose of indentation in python:

"Infinite" and "Finite" While Loop

Before that, let's look into boolean datatype. In Python, the values supported for the said are True and False. Python is a case sensitive language. So you have to be careful about it. The first letter is capitalized here 'T' and 'F'

We can have local operators too. like and, or and not just as you do in other languages. For example in C or C++ the equivalent operators are &&, ||

Sometimes it is convenient to use infinite loop and to break the loop when certain conditions are met.

for example we will print 1 to 10 using an infinite loop. Whenever the varibale reach the value 10 the loop will break Let's try!

CAUTION: IF YOU RUN THIS CODE IT'LL START A NEVER ENDING (i.e. INFINITE) LOOP.

To STOP, CLICK ON THE SQUARE BOX AT THE TOP OF YOUR SCREEN

num = 0
while True:
    print(num)
    num = num + 1

We can do the same job in a different way:

general syntax:

while(logical expression):
    statements
    ...
    ...

Now let's look into the familiar for loop

Syntax:

for var in range(start, end, step):
    statements
    ...
    ...

start and step arguments in the range are optional. By default those are set to be 0 and 1 respectively.

Let's talk about function. Perhaps the the second last building block of lerning a new language. Immediately after LEARNING the function you'll able to do the regular numerical program you used to write for your academic purpose.

Syntax:

def name(arguments):
    statements
    ...
    ...
    retuen the_resut

let's have a simple addition function.

We can have another interesting example. "Collatz" sequence.

The idea is; take any arbitrary integer. If it is even, divide it by 2 otherwise multiply the number by 3 and then add 1 to it. Repeat this process. It'll get terminated at n=1. But nobody knows why!

So, we are at the very end of getting aquainted with a new programming language.

For any new language you want to learn, the basic steps are the same.

........................... And you're done!

Now, Let's talk about some inbuilt data structures

List

These are mutable, that is we can edit the elements of the list. Lists can contain heterogeneous data unlike C/C++/Fortran array. We denote list by a pair of brackets. Example;

We can iterate over list by two different ways,

i. By index

ii. By the elements of the list itself

** NOTE: List index starts from 0

Let's have a 2-D list, that is a list of a list

We will go simple. Say, we have an idendity matrix

As we have said lists are mutable, let's have an example.

Also note that string and tuple are immutable.

You can convert one data type to another with smple built-in functions. like: tuple(var), list(var), string(var)

You see, it's not allowed. You can try to convert that tuple or list to a string and can try to do the same. String element can be accessed in the same manner.

Say if you have a string variable, name='python' then name[2] will return 'y'0. Try yourself!

Dictionary and all

Unlike string, tuple and list, dictionary are consists of key-value pairs. keys are string type data where as values could be anything - lists, string, tuples and even can be another dictionary. We will go easy. Let's say, you want to design a game and you want to keep track of gems, lifes and the user name of the player. btw, dictionaries are mutable objects!

In the session I was unable to show you issmall thins with strings. Actual command isislower() and isupper()! My bad, sorry! :")

you can try the reverse

let's look into some other methods

isalpha - is the string is consists of anly alphabets

isalnum - is the string consists of alphanumeric type data

isdecimal - is the string consists only of decimals

The very last thing we did, we imported pyperclip

What it does? It is helpful to access your clip board

Before running the block below, copy some text from anywhere you like

That's it for today!!

Practise this elementary stuffs. We will upload the problem set soon, where you need to show some of your creativity to solve those problems. Remember, writing a code is just the 5%; the rest is bug fixing where the learning starts. Before start writing a code, try to write down the idea on paper, how you'll solve the problem. If you do that, you've already done with the 50% of your task. Also, we will introduce you to the random library in the problem set. Don't worry; we will always be there to solve your confusion and to make you comfortable. You're just an email away!

P.S. We'll try to make the next session less messy. ROFL!