Tuesday, December 12, 2017

Lists in Python

So Lists,some time we want to form multiple objects under a single variable name.To define a list in python.you have to do something like this.

eg:
student_names = []

simple you have created empty student names list.if you want to add some student names then simple replace empty brackets like this

student_names = ["Abhinaw", "Rajat', "Gaurav", "Babli"]

now our list has 4 string elements.In order to access the names from list we use something called index staring from 0.
eg:

student_names[0] == "Abhinaw"
student_names[2] == "Gaurav"

So suppose you want to get last element from the list.then you have to do something like this.

student_names[-1] == "Babli"

So actually this gives the first element from right side of the list.In Python just remember there is no such thing negative zero.

                  List Functions

suppose you want add some names to list then you have to do like this.

eg:

student_names = ["Abhinaw", "rajat", "Gaurav"]

student_names[3] = "Babli" # No can do!

student_names.append("Babli") # Add to the  end

So now list will be:

student_names == ["Abhinaw", "rajat", "Gaurav", "Babli"]

*if you want to check the name already exist or not

eg:

"Abhinaw" in student_names == True

# Abhinaw is still there

*check how many elements in the list
eg:

len(student_list) == 4

Note: if you have noticed in Python we have never tell that it is list of string.So this is added advantage with the Python over other programming language.

    Delete element from List

eg:

students_names = ["abhinaw", "rajat", "Gaurav"]

del student_names[2]

this will delete 2nd element from list
which is "Gaurav" .Now the whole list will be going to shift after this operation.

                       List Slicing

student_names = ["Mark" ,"Katarina" ,"Homer"]

let say we want to ignore the first element which is "Mark" and get all other.

student_names[1:] == ["Katarina", "Homer"]

student_names[1:-1] == ["Katarina"]
Note this will not modify the list.list will be same it is just a temporary ignoring certain elements.

there are many more operations you can perform like reverse and pop etc.




If Statements in Python

If Statement is very simple in Python.Let me give you an example.

number = 5
if number == 5:
       print("Number is 5")
else:
       print("Number is NOT 5")

Remember one thing Python uses indentation rather than curly braces for any code blocks.notice that there is colon(:) at if and else both.this is required in python.we use == equal sign to check the  equality in Python.

in Python there is a concept of "Truthy and Falsy Values" for example.

number=5
if number:
 
     print("Number is defined and truthy")

text = "Python"
if text:
   
    print("Text is defined and truthy")

In many other programming language you have to check the length of the variable if it is value greater or less than zero but in Python you could just write like above.

okay in case of Boolean and None:it also has Truthy and Falsy Values.
eg:

pyhton_course = True

if python_course :

# Not python_course == True

    print("this will execute")

aliens_found = None

if aliens_found :
    print("This will NOT execute")

you can also set the same thing for None like I have done above.

               Not If

number = 5

if number !=5 :
      print("this will not execute")

python_course = True

if not python_course:

     print("this will also not excute")

Multiple If Conditions

number = 3

python_course = True

if number  == 3 and python_course:
   
    print("this will execute")

if number == 17 or python_course :

   print("this will also execute")

Note: in Python there is no && or || for and  and or like in other programming language.

           Ternary If Statements

a = 1
b = 2

"bigger" if a>b else "smaller"

This is called Ternary if Statement in Python.

Boolean and None in Python

As you know about boolean in other programming language. it is same in python too but with small differences .boolean can be  a true or false value.can be declared as either of them.

eg:

python_course = True
java_course = False

Simply type a name and assign a value "True" or False .

if you have noticed only difference with other programming language is that it starts with Capital letter T for True and Capital letter F for False .interestingly you can convert Boolean value to int and it will give you values 1 and 0 in case of True and False like below.

eg:

int(python_course) == 1
int (java_course) == 0
str(python_course) == "True"

And for string it will simply give textual representation "True" or "False".

Now coming to None: it is similar to null like other programming languages.

eg:

aliens_found = None

I find it useful as a place holder variable.Something that I will use later.However there is difference between variable you have defined None and other the  variable not defined at all.
Simply type k in idle and idle will tell you that k has not been defined but if I assign None to k then it will not give any result but normal for None and also it will not cause any error to show either.So None is called None type.

Monday, December 11, 2017

Strings in Python

We use string to represent text.by default it's Unicode text in Python 3 but ASCII text in Python 2.And this is one of  the big advantage in Python 3 by the way.Strings can be defined like below:

eg:

'hello world' == "Hello World" == " " "Hello World " " "

There is absolutely no difference which one you use.you can write multi-line string using 3 double quotes but this string  is not going to assign to any variable.it just to be sitting there as a comment.your editor will recognize it.it is a comment.Definning String is very simple just type a variable name and assign it a textual value  and done.
String in Python supports lot of utility methods.some of them are:

eg:

"hello" .capitalize() == "Hello"
"hello".replace("e" , "a") == "hallo"
"hello" .isalpha() == True
"123".isdigit() == true # Usefully when converting to int

"some ,CSV,values" .split(",") == ["Some" , "CSV" , "Values"]

String Format Function-

normally, use when you want to replace existing text in a given string with a variable value.

For example:

name = "pyhthonBO"
machine = "HAL"

Let's print it like this:

"Nice to meet you {0}.I am {1}".formate(name,machine)

So you must be thinking it of bit weird.So let me tell you here {0} and {1} meaning.
{0} represents the variable name argument position in format function.
you can add many argument to the formate function.
python 3.6 introduces string interpolation though So now you can do some thing like

f"Nice to meet you{name}.I am {machine}"

you can also pre-fix with r and u before the string.to deal with raw and Unicode text.its up to you.what you choose.
There are many other string properties like slicing and cutting etc.

Integers and Floats in Python

Defining integers in Python is very easy.
eg:
answer=42

pi=3.14159

Python 3 also introduces complex numbers as a type.There is no real need to worry about types in Python.atleast not in the beginning.

answer + pi = 45.14159 # Don't worry about conversion !

you can seamlessly add integers, float .pyhton does not complain about it.it will produce desired results.And to cast anything.just have to do something like this.

int(pi) == 3
float(answer) == 42.0

this is needed when we really want to cast the integer and float variables.

Thursday, December 7, 2017

Types in Python

In Python type has been handled differently than other programming languages.in many programming languages including JAVA or C# .you have to declare variable type before you declare variable itself.that variable type can be int,float,double, String,char Boolean or your own custom types such as Student.So before you even declare variable you have to write something like this.

// C# or java

int answer=42;
String name="Pyhton";

With pyhton take this approach in opposite direction.in pyhton it is not necessary  to declare type anywhere.in fact you will do something like this.

# Python

answer = 42
name = "Python'

And same thing goes with other types including your own type.
Now there are some advantages and disadvantages of this approach.
Pyhton also called dynamically typed language.So not declaring types leads many bugs for example suppose we have simple function they just adds two numbers.will look like below.

def add_numbers(a,b)
print(a+b)

Call the function
add_numbers(5,11)
16. everyhting is fine.

but what if we call like below:

add_numbers(5,"something")

It will give type error:unsupported operand types.
well python does not have any machenism to tell you in advance while other languages have.

There is something called "type hinting".

Type Hinting: this is new features added in Python version 3.5 .they will help you to add data types in your program.it will help your IDE.it will not prevent your wrong  code running.So you can do something like this:

def add_numbers(a: int, b: int)  -> int:
return  a + b

Installing PyCharm

Although we have started using IDLE for python.now we will use PyCharm integrated Developer environment.this is excellent IDE and this is built by the same forks who brought  IntelliJ ide.They have a free community edition for PyCharm available for download from there Website.
If you don't want to use PyCharm don't use it. any text editor will work fine including notepad.for the purpose I will certainly give all tutorials using PyCharm.

To download it simply go to"www.jetbrains.com/pycharm/" then click on the "Download Now" button and then "Download" free community edition of PyCharm.once you download and install PyCharm.click "Create New Project" and make sure that "Pure Python" is selected.
also you can see Location and Interpreter path.if you click on Interpreter you will see all the installed pyhton interpreter on your computer. And also make sure you select Python version 3 and above.in Location path basically it's your workspace.
now click on create.then you will be able to see Pycharm IDE with some files in it.And you can also add Python files by right click on your project and select pyhton file,name it helloworld.py  and click ok and write below code.

print("Hello World") and then right click on the HelloWorld.py you will see "Run helloworld'". in consloe you will able to see Hello World .

Now your PyCharm IDE is ready to use.

The Awesomeness of the Python

One you install python.you will have a handy tool called PowerShell or python terminal.if you just type Python 3 then you will have an access of entire pyhton programming language.this is pretty much different than any other programming languages such as Java ,C and if you are a front end developer.think of this as JavaScript console in your browser developer tools.

So what it mean exactly?.

Well being able to just type Python in terminal gives you some advantages as a developer.

for example,
suppose you are not really sure about a result of a function or any other code out put.you can simply start the Python interpreter on your machine and see what happening when you used that function.Now a days many pyhton I'd is coming with integrated pyhton console.thats the same when we ran the program through our command prompt terminal.
unfortunately this also comes with a draw back every Python program we ran pre-fix it with the Python command.

let say we  have a sample program:

# hello.py
print("Hello World")

It simply prints Hello World but in order to run it we need to open terminal and write
hello.py

And you will see below output
Hello World

During development it is a distraction but now many Python I'd are coming in a single package.

Towards the end of the course I will teach you how to create one binary file from windows,Mac or Linux machine. then you can run on any operating system.as I have mentioned if you use python from command line you will have full power of python  but if you want little bit more than that such as Syntax highlighting then you will want to run the "idle" integrated development environment.
Not that this not just any IDE .this is comes installed by default.
To open it just simply search it idle and tap it,CMD will be opened . there is not much difference when you were using CMD.
The advantages of using IDLE over CMD(Command Prompt) is code completion,Syntax highlighting etc.
Very soon I will tell you about a free integrated development environment called PyCharm.