What is Python?
Python is a general-purpose programming language created by Guido Van Rossum. Python is most praised for its elegant syntax and readable code, if you are just beginning your programming career Python suits you best.
With Python you can do everything from
GUI (graphical user interface ) development,
Web application,
System administration tasks,
Financial calculation,
Data Analysis,
Visualization
and list goes on…
What doeas it mean that Python is interpreted language?
Język interpretowany – język programowania, w którym interpreter analizuje program linia po linii. Przeciwieństwem języków interpretowanych są języki implementowane w postaci kompilatora (kompilator najpierw kompiluje cały program, a następnie zaczyna działać). Języki interpretowane są nieco wolniejsze od języków kompilowanych, lecz prostsze do napisania (mniej rzeczy o które trzeba się martwić podczas pisaniaskodu)
Python is interpreted language, when you run python program an interpreter will parse python program line by line basis, as compared to compiled languages like C or C++, where compiler first compiles the program and then start running.
Now you may ask, so what’s the difference??
The difference is that interpreted languages are a little bit slow as compared to compiled languages. Yes, you will definitely get some performance benefits if you write your code in compiled languages like C or C++.
But writing codes in such languages is a daunting task for a beginner. Also in such languages, you need to write even most basic functions like calculate the length of the array, split the string etc. For more advanced tasks sometimes you need to create your own data structures to encapsulate data in the program. So in C/C++ before you actually start solving your business problem you need to take care of all minor details. This is where Python comes. In Python, you don’t need to define any data structure, no need to define small utility functions because Python has everything to get you started.
Moreover, Python has hundreds of libraries available at https://pypi.python.org/ which you can use in your project without reinventing the wheel.
What does it mean that Python is Dynamically Typed ?
Python sam automatycznie rozpoznaje typ zmiennych na podstawie kodu.
Python doesn’t require you to define variable data type ahead of time. Python automatically infers the data type of the variable based on the type of value it contains.
For e.g:
myvar = “Hello Python”
The above line of code assigns string “Hello Python” to the variable myvar, so the type of myvar is string.
Note that unlike languages like C, C++ and Java, in Python you do not need to end a statement with a semicolon (;).
Suppose, a little bit later in the program we assign variable myvar a value of 1 i.e
myvar = 1
Now myvar variable is of type int.
What does it mean that Python is strongly typed?
Nie dokonuje on automatycznej konwersji kodu.
If you have programmed in PHP or javascript. You may have noticed that they both convert data of one type to another automatically.
For e.g:
In JavaScript
1 + “2”
will be ‘12’
Here, before addition (+) is carried out, 1 will be converted to a string and concatenated to “2”, which results in ‘12’, which is a string. However, In Python, such automatic conversions are not allowed, so
1 + “2”
will produce an error.
Which code will be longer, the one written in Python or Java?
Programs written in Python are usually 1/3 or 1/5 of the Java code. It means we can write less code in Python to achieve the same thing as in Java.
Who uses Python?
Python is used by many large organizations like Google, NASA, Quora, HortonWorks and many others.
Okay, what I can start building in Python?
Pretty much anything you want. For e.g:
GUI applications.
Web apps.
Scrape data from websites.
Analyse Data.
System administration utilities.
Game Development.
Data Science
and many more …
Do I have to install Python on my PC?
Mac also comes with python 2 and python 3 installed (if not see this link for instructions), but this is not the case with windows. Similarly, most Linux distribution for e.g Ubuntu 14.04 comes with python 2 and 3 installed, bu you have to install Python on Windows
How to install Python on Windows/Ubuntu/Mac?
To install python you need to download python binary from https://www.python.org/downloads/, specifically we will be using python 3.4.3 which you can download from here. While installing remember to check “Add Python.exe to path”
If you are using Ubuntu 14.04 which already comes with python 2 and python 3, you need to enter python3 instead of just python to enter python 3 shell. Also you will need a text editor to write Python programs, you can use text editor like notepad. If you want to use full-fledged text editor then use notepad++ or sublime text. Download and install text editor of you choice.
On Mac is good to check Python version by typing in terminal: ~ % python3 –version
Then to install Python 3 with the Official Installer from: https://www.python.org/downloads/
How to run python programs?
You can run python programs in two ways, first by typing commands directly in python shell:
Np.
print(“Hello World”)
> Hello World
or run program stored in a file. But most of the time you want to run programs stored in a file.
Np. do terminala wpisujemy:
python hello.py
W ten sposób wgrywamy plik z zapisanym tam uprzednio programem.
How many arguments does print() function have?
as many arguments as you provide it with (as many as editor allow). When two or more arguments are passed, the print() function displays each argument separated by space.
Np.
print(“Hello World”)
> Hello World
print(“Hello”, “World”)
> Hello World
Which command is used to change directory?
cd ‘od change directory’
Open terminal and change current working directory to C:\Users\YourUserName\Documents using cd command
What to do when you want to know more about some method or functions?
Type help() into the terminal.
Sooner or later while using python you will come across a situation when you want to know more about some method or functions. To help you Python has help() function, here is how to use it.
Syntax:
To find information about class: help(class_name)
To find more about method belong to class: help(class_name.method_name)
Now suppose you want to know arguments required for index() method of str class, to find out you need to type the following command in the python shell: help(str.index)
What are variables in Python and what are the rules of naming them?
Variables are named locations that are used to store references to the object stored in memory. The names we choose for variables and functions are commonly known as Identifiers. In Python, Identifiers must obey the following rules.
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
pass else import assert
break except in raise
Values or literals?
Values = literals
They are basic things that programs work with. For e.g: 1, 11, 3.14, “hello” are all values.
They are also commonly known as literals.
They can be of different types for e.g 1, 11 are of type int, 3.14 is a float and “hello” is a string.
What is a object in Python?
Remember that in Python everything is object even basic data types like int, float, string,
How to assign value to a variable?
To assign value to a variable equal sign (=) is used. The = sign is also known as the assignment operator.
Should I declare types of variables while using Python?
In Python, you don’t need to declare types of variables ahead of time. The interpreter automatically detects the type of the variable by the data it contains.
Show some examples of variable declaration
x = 100 # x is integer
pi = 3.14 # pi is float
sentence = “python is great” # sentence is string
a = b = c = 100 # this statement assign 100 to c, b and a.
What the variable stores?
When a value is assigned to a variable, the variable doesn’t store the value itself. Instead, the variable only stores a reference (address) of the object where it is stored in the memory.
Therefore, in the x = 100, the variable x stores a reference (or address) to the 100 ( an int object ). The variable x doesn’t store the object 100 itself.
Comments
Comments are notes which describe the purpose of the program or how the program works. Comments are not programming statements that Python interpreter executes while running the program. Comments are also used to write program documentation. In Python, any line that begins with a pound sign (#) is considered a comment.
e.g:
#This program prints “hello world”
print(“hello world”)
We can also write comments at the end of a statement:
print(“hello world”) # display “hello world”
What are end-line comments?
Comments that appear in this form:
print(“hello world”) # display “hello world”
simultaneous assignment
The simultaneous assignment or multiple assignment allows us to assign values to multiple variables at once. The syntax of simultaneous assignment is as follows:
var1, var2, …, varn = exp1, exp2, …, expn
e.g:
a, b = 10, 20
print(a)
> 10
print(b)
>20
Simultaneous assignments is quite helpful when you want to swap the values of two variables.
e.g:
x = 1 # initial value of x is 1
y = 2 # initial value of y is 2
y, x = x, y # assign y value to x and x value to y
print(x) # final value of x is 2
print(y) # final value of y is 1
> 2
1
Name Python data types
List values in Python, which are considered as false
Boolen False
0 - zero , 0.0
[] - empty list ,
() - empty tuple ,
{} - empty dictionary ,
‘ ‘ - empty string
None