Posted in Home

Control Flow

Python if else

There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arises in programming also where we need to make some decisions and based on these decisions we will execute the next block of code.

Decision making statements in programming languages decides the direction of flow of program execution. Decision making statements available in python are:

  • if statement
  • if..else statements
  • nested if statements
  • if-elif ladder
  • Short Hand if statement
  • Short Hand if-else statement

if statement

if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Here, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also.

As we know, python uses indentation to identify a block. So the block under an if statement will be identified as shown in the below example:

if condition:               # Statements to execute if    # condition is true
if-statement-in-java

if- else

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:

if (condition):
    # Executes this block if
    # condition is true
else:
    # Executes this block if
    # condition is false

Flow Chart:-

Posted in Home

Functions

Functions in Python

Last Updated: 11-09-2018

A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.
Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are called user-defined functions.

# A simple Python function to check 
# whether x is even or odd 
def evenOdd( x ): 
if (x % 2 == 0): 
print "even"
else: 
print "odd"
 
# Driver code 
evenOdd(2) 
evenOdd(3) 

Output:

even
odd

Pass by Reference or pass by value?
One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. Parameter passing in Python is same as reference passing in Java.

# Here x is a new reference to same list lst 
def myFun(x): 
x[0] = 20
 
# Driver Code (Note that lst is modified 
# after function call. 
lst = [10, 11, 12, 13, 14, 15]  
myFun(lst); 
print(lst) 

Output:

[20, 11, 12, 13, 14, 15]
Posted in Home

Python OOP

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
To understand the need for creating a class let’s consider an example, let’s say you wanted to track the number of dogs which may have different attributes like breed, age. If a list is used, the first element could be the dog’s breed while the second element could represent its age. Let’s suppose there are 100 different dogs, then how would you know which element is supposed to be which? What if you wanted to add other properties to these dogs? This lacks organization and it’s the exact need for classes.
Class creates a user-defined data structure, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
Some points on Python class:
Classes are created by keyword class.
Attributes are the variables that belong to class.
Attributes are always public and can be accessed using dot (.) operator. Eg.: Myclass.Myattribute


<strong>Class Definition Syntax:</strong> class ClassName:     
# Statement-1     
.     .     .     
# Statement-N 
<strong>Defining a class –</strong>

python class

Declaring Objects (Also called instantiating a class)
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.
Example:

python declaring an object

Declaring an object


# Python program to
# demonstrate instantiating
# a class


classDog: 

# A simple class
# attribute
attr1 ="mamal"
attr2 ="dog"

# A sample method  
deffun(self): 
print("I'm a", self.attr1)
print("I'm a", self.attr2)

# Driver code
# Object instantiation
Rodger =Dog()

# Accessing class attributes
# and method through objects
print(Rodger.attr1)
Rodger.fun()

Output:
mamal I’m a mamal I’m a dog
In the above example, an object is created which is basically a dog named Rodger. This class only has two class attributes that tell us that Rodger is a dog and a mammal.
The self
Class methods must have an extra first parameter in method definition. We do not give a value for this parameter when we call the method, Python provides it.
If we have a method which takes no arguments, then we still have to have one argument.
This is similar to this pointer in C++ and this reference in Java.
When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.
__init__ method:
The __init__ method is similar to constructors in C++ and Java. Constructors are used to initialize the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.

# A Sample class with init method 
classPerson: 
# init method or constructor  
def__init__(self, name): 
self.name =name 
# Sample Method  
defsay_hi(self): 
print('Hello, my name is', self.name)
p =Person('Nikhil') 
p.say_hi() 

 
Output:
Hello, my name is Nikhil
Class and Instance Variables
Instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class. Instance variables are variables whose value is assigned inside a constructor or method with self whereas class variables are variables whose value is assigned in the class.
Defining instance varibale using constructor.

# Python program to show that the variables with a value  
# assigned in the class declaration, are class variables and 
# variables inside methods and constructors are instance 
# variables. 

# Class for Dog 
classDog: 

# Class Variable 
animal ='dog'

# The init method or constructor 
def__init__(self, breed, color): 

# Instance Variable     
self.breed =breed
self.color =color        

# Objects of Dog class 
Rodger =Dog("Pug", "brown") 
Buzo =Dog("Bulldog", "black") 

print('Rodger details:')   
print('Rodger is a', Rodger.animal) 
print('Breed: ', Rodger.breed)
print('Color: ', Rodger.color)

print('\nBuzo details:')   
print('Buzo is a', Buzo.animal) 
print('Breed: ', Buzo.breed)
print('Color: ', Buzo.color)

# Class variables can be accessed using class 
# name also 
print("\nAccessing class variable using class name")
print(Dog.animal)        

Output:
Rodger details: Rodger is a dog Breed: Pug Color: brown Buzo details: Buzo is a dog Breed: Bulldog Color: black Accessing class variable using class name dog
Defining instance variable using the normal method.

# Python program to show that we can create  
# instance variables inside methods 

# Class for Dog 
classDog: 

# Class Variable 
animal ='dog'

# The init method or constructor 
def__init__(self, breed): 

# Instance Variable 
self.breed =breed             

# Adds an instance variable  
defsetColor(self, color): 
self.color =color 

# Retrieves instance variable     
defgetColor(self):     
returnself.color    

# Driver Code 
Rodger =Dog("pug") 
Rodger.setColor("brown") 
print(Rodger.getColor())  
Output:
brown

Posted in Home

Python CSV

Working with csv files in Python

csv3

This article explains how to load and parse a CSV file in Python.

First of all, what is a CSV ?
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.

For working CSV files in python, there is an inbuilt module called csv.

Reading a CSV file

# importing csv module 
import csv 

# csv file name 
filename = "aapl.csv"

# initializing the titles and rows list 
fields = [] 
rows = [] 

# reading csv file 
with open(filename, 'r') as csvfile: 
	# creating a csv reader object 
	csvreader = csv.reader(csvfile) 
	
	# extracting field names through first row 
	fields = next(csvreader) 

	# extracting each data row one by one 
	for row in csvreader: 
		rows.append(row) 

	# get total number of rows 
	print("Total no. of rows: %d"%(csvreader.line_num)) 

# printing the field names 
print('Field names are:' + ', '.join(field for field in fields)) 

# printing first 5 rows 
print('\nFirst 5 rows are:\n') 
for row in rows[:5]: 
	# parsing each column of a row 
	for col in row: 
		print("%10s"%col), 
	print('\n') 

The output of above program looks like this:

csv1

The above example uses a CSV file aapl.csv which can be downloaded from here.
Run this program with the aapl.csv file in same directory.

Let us try to understand this piece of code.

  • with open(filename, ‘r’) as csvfile: csvreader = csv.reader(csvfile)Here, we first open the CSV file in READ mode. The file object is named as csvfile. The file object is converted to csv.reader object. We save the csv.reader object as csvreader.
  • fields = csvreader.next()csvreader is an iterable object. Hence, .next() method returns the current row and advances the iterator to the next row. Since the first row of our csv file contains the headers (or field names), we save them in a list called fields.
  • for row in csvreader: rows.append(row)Now, we iterate through remaining rows using a for loop. Each row is appended to a list called rows. If you try to print each row, one can find that row is nothing but a list containing all the field values.
  • print(“Total no. of rows: %d”%(csvreader.line_num))csvreader.line_num is nothing but a counter which returns the number of rows which have been iterated.

Writing to a CSV file

# importing the csv module 
import csv 

# field names 
fields = ['Name', 'Branch', 'Year', 'CGPA'] 

# data rows of csv file 
rows = [ ['Nikhil', 'COE', '2', '9.0'], 
		['Sanchit', 'COE', '2', '9.1'], 
		['Aditya', 'IT', '2', '9.3'], 
		['Sagar', 'SE', '1', '9.5'], 
		['Prateek', 'MCE', '3', '7.8'], 
		['Sahil', 'EP', '2', '9.1']] 

# name of csv file 
filename = "university_records.csv"

# writing to csv file 
with open(filename, 'w') as csvfile: 
	# creating a csv writer object 
	csvwriter = csv.writer(csvfile) 
	
	# writing the fields 
	csvwriter.writerow(fields) 
	
	# writing the data rows 
	csvwriter.writerows(rows)

Let us try to understand the above code in pieces.

  • fields and rows have been already defined. fields is a list containing all the field names. rows is a list of lists. Each row is a list containing the field values of that row.
  • with open(filename, ‘w’) as csvfile: csvwriter = csv.writer(csvfile)Here, we first open the CSV file in WRITE mode. The file object is named as csvfile. The file object is converted to csv.writer object. We save the csv.writer object as csvwriter.
  • csvwriter.writerow(fields)Now we use writerow method to write the first row which is nothing but the field names.
  • csvwriter.writerows(rows)We use writerows method to write multiple rows at once.

Writing a dictionary to a CSV file

# importing the csv module 
import csv 

# my data rows as dictionary objects 
mydict =[{'branch': 'COE', 'cgpa': '9.0', 'name': 'Nikhil', 'year': '2'}, 
		{'branch': 'COE', 'cgpa': '9.1', 'name': 'Sanchit', 'year': '2'}, 
		{'branch': 'IT', 'cgpa': '9.3', 'name': 'Aditya', 'year': '2'}, 
		{'branch': 'SE', 'cgpa': '9.5', 'name': 'Sagar', 'year': '1'}, 
		{'branch': 'MCE', 'cgpa': '7.8', 'name': 'Prateek', 'year': '3'}, 
		{'branch': 'EP', 'cgpa': '9.1', 'name': 'Sahil', 'year': '2'}] 

# field names 
fields = ['name', 'branch', 'year', 'cgpa'] 

# name of csv file 
filename = "university_records.csv"

# writing to csv file 
with open(filename, 'w') as csvfile: 
	# creating a csv dict writer object 
	writer = csv.DictWriter(csvfile, fieldnames = fields) 
	
	# writing headers (field names) 
	writer.writeheader() 
	
	# writing data rows 
	writer.writerows(mydict) 

In this example, we write a dictionary mydict to a CSV file.

  • with open(filename, ‘w’) as csvfile: writer = csv.DictWriter(csvfile, fieldnames = fields)Here, the file object (csvfile) is converted to a DictWriter object.
    Here, we specify the fieldnames as an argument.
  • writer.writeheader()writeheader method simply writes the first row of your csv file using the pre-specified fieldnames.
  • writer.writerows(mydict)writerows method simply writes all the rows but in each row, it writes only the values(not keys).

So, in the end, our CSV file looks like this:

csv2

Important Points:

  • In csv modules, an optional dialect parameter can be given which is used to define a set of parameters specific to a particular CSV format. By default, csv module uses excel dialect which makes them compatible with excel spreadsheets. You can define your own dialect using register_dialect method.
    Here is an example:
     csv.register_dialect(
    'mydialect',
    delimiter = ',',
    quotechar = '"',
    doublequote = True,
    skipinitialspace = True,
    lineterminator = '\r\n',
    quoting = csv.QUOTE_MINIMAL)

Now, while defining a csv.reader or csv.writer object, we can specify the dialect like
this:

csvreader = csv.reader(csvfile, dialect='mydialect')
  • Now, consider that a CSV file looks like this in plain-text:
    We notice that the delimiter is not a comma but a semi-colon. Also, the rows are separated by two newlines instead of one. In such cases, we can specify the delimiter and line terminator as follows:csvreader = csv.reader(csvfile, delimiter = ';', lineterminator = '\n\n')
Posted in Home

Input/Output

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard.

  • input ( type your input here)
  • raw_input ( type your input here )

input ( ) : This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by python. For example:

  • Các nhà phát triển thường có nhu cầu tương tác với người dùng, để lấy dữ liệu hoặc cung cấp một số loại kết quả. Hầu hết các chương trình ngày nay đều sử dụng hộp thoại như một cách để yêu cầu người dùng cung cấp một số kiểu đầu vào.
  • Trong khi Python cung cấp cho chúng ta hai hàm có sẵn để đọc đầu vào từ bàn phím.
    • input (lời nhắc)
    • raw_input (lời nhắc) input ():
  • Hàm này trước tiên lấy đầu vào từ người dùng và sau đó đánh giá biểu thức, có nghĩa là Python tự động xác định xem người dùng đã nhập một chuỗi hay một số hoặc danh sách. Nếu đầu vào được cung cấp không chính xác thì một trong hai lỗi cú pháp hoặc ngoại lệ được đưa ra bởi python. Ví dụ :
Python program showing
 a use of input()
 val = input("Enter your value: ") 
 print(val) 
Posted in Home, Life in Europe

Work-life balance of Software Engineer in Germany

The issues around finding the balance between family life, private life and work are gaining increased attention in political and business circles in Europe and Germany.

A key issue for many workers is flexible working time in order to have a work-life balance. Negotiating a work/life balance can help enable parents (both men and women) to reconcile their work with their family lives and women in particular to participate in the labour market. Finding the right work-life balance can allow workers to take leave from work so that they can participate in education or training or take up an interest, hobby or leisure pursuit. This may mean that employees can reorganise their working lives and hours around shorter days, weeks, months or years.

German families tend to be small with only one or two children. The men are still quite often considered to be the head of the household, even though both the wife and husband work.

At the turn of the century few employees in Germany were given holidays. In 1902, the metal and brewing industries gave three days annual leave to their workers. It was not until 1974 that the old Federal Republic introduced the statutory minimum holiday of 18 working days which has now risen to a minimum of 24 days. Today most collective wage agreements provide for holidays of six weeks or more and most employers give  holiday pay.

National holidays

Germany has quite generous holidays in comparison to other European countries. There are more public holidays in Germany than in any other European country. On these days, banks and most shops are closed, including supermarkets. However, many restaurants remain open. Public transportation and other services are also available. Many shops and businesses are also closed on Carnival Rose Monday (Cologne and Rhine region), Christmas Eve and New Year’s Eve although these are not official holidays.

Overview of legal holidays:

New Year                
Epiphany                   
Good Friday              
Easter Monday               
Labour Day               
Ascension                  
Whit Monday             
Corpus Christi                       
Assumption Day        
Day of German Unity            
Reformation Day       
All Saints’ Day                       
Penance Day              
Christmas                  
St. Stephen’s Day
01.01
06.01   (celebrated in BW, BY, ST)
around March/April
around March/April
01.05
May
May
May/June (celebrated in  BW, BY, HE, NW, RP, SL)
15.08   (celebrated in  BY, SL)
03.10
31.10   (celebrated in  BB, MV, SN, ST, TH)
01.11   (celebrated in BW, BY, NW, RP, SL
21.11   (celebrated in SN)
25.12
26.12

(Those States where the public holiday applies are shown in brackets; if nothing is indicated the holiday applies to all of Germany.)

Working hours

Opening hours

In Germany, businesses and shops are not legally allowed to stay open as long as they please and there are strict regulations concerning opening and closing hours. The German federal law “Ladenschlussgesetz” (Shop Closing Law) together with individual regulations in different States controls opening hours. Thus supermarkets for example close at 22.00 at the latest and open before 9 a.m. or 10 a.m. On Sundays almost everything is closed with the exception of bakeries and petrol stations.

 Working times

The German Working Time Regulations (“Arbeitszeitgesetz”) regulate working hours on a legal basis. They are based on the European regulation 93/104/EG. In addition, most industries have collective agreements that regulate working hours and holidays. However, it can be said, that a working week of more than 48 hours on average during a  6 month period must not be exceeded. Furthermore, Sundays and national holidays are non-working days.

Working culture

Germans see themselves as modern, liberal and cultured, and working practices are formal and professional. The following outlines the working practices that you should be familiar with before investing in Germany:

  • Though long-term relationships are considered very important, friendships are usually not developed too quickly. It may take some time before personal names are used between non-familial parties.
  • German business culture has a well-defined and strictly observed hierarchy, with clear responsibilities and distinctions between roles and departments.
  • Professional rank and status in Germany is generally based on an individual’s achievement and expertise in a given field. Academic titles and backgrounds are important, conveying an individual’s expertise and thorough knowledge of their particular area of work.
  • An important aspect is Germany’s work ethic. Employees define themselves as part of the corporation they are working for and quickly identify themselves with its product and/ or services.
  • Rank is very important in business. Never set up a meeting for a lower ranked company employee to meet with a higher ranked person.
  • Notwithstanding what has been said previously, today over half of all university graduates are women. Female students are well represented in the professions; they lead in some fields such as medicine and law. The new availability of qualified female graduates is likely to bring great changes in the German workplace of the future.
  • Pay and power inequalities are still present however. Male employees tend to receive higher wages than their female counterparts. Jobs considered as being “women’s work” typically pay less than those deemed “men’s work”.
  • In more traditional companies, it is still generally true that everything is run by committees, things are discussed in great length and risk taking is not as common as in other countries.
  • There is one philosophy for almost everybody in German business: if someone says he is going to do something, he will do it. The same is expected of others as well. Never make a promise that you cannot keep or offer something that you cannot deliver. Germans dislike and do not trust unreliable people.
  • There is no legislated or administratively determined minimum wage. Collective bargaining agreements set minimum pay rates and are enforceable by law for an estimated 80 to 90 per cent of all wage and salary earners
  • Federal regulations limit the working week to a maximum of 48 hours, but collective bargaining agreements may supersede these. Contracts that directly or indirectly affect 80% of the working population regulate the number of hours of work per week.
  • The average working week is around 40 hours; rest periods for lunch are accepted practice. Provisions for overtime, holidays, and weekend pay vary depending upon the applicable collective bargaining agreement.
  • An extensive set of laws and regulations govern occupational health and safety. A comprehensive system of worker insurance enforces safety requirements in the workplace.

It is important that these issues are examined and understood before setting up a company and employing a workforce in Germany. These issues differ all over Europe but legal guidelines are set by the European Commission.

Health insurance

Germany’s health care system provides its residents with nearly universal access to comprehensive high-quality medical care and a choice of physicians. Over 90% of the population receives health care through the country’s statutory health care insurance programme. Membership of this programme is compulsory for all those earning less than a periodically revised income ceiling. Nearly all of the remainder of the population receives health care via private for-profit insurance companies. Everyone uses the same health care facilities.

Posted in Home

Live*

This is a page to share my thoughts and feelings about new events, happening live today or in the near future, all I share is my personal thoughts, looking forward to discussing with everyone.

VIDEO] Live Stream Best Practices (1/2) | Believe

Đây là chuyên mục cập nhật thường ngày, nơi mình đưa ra những bài viết về những vấn đề nổi cộm hiện nay, tất nhiên là dưới góc nhìn của một lập trình viên kinh nghiệm đang làm việc tại 1 trong những công ty vận tải lớn nhất thế giới:

Nội dung chia sẻ bao gồm:

Lập trình

Khoa học máy tính

Thông tin kinh tế, chính trị xã hội

Hoặc các chủ đề khác nếu nhiều bạn thích nói về nó thì mình sẽ viết bài trên này hoặc là làm vlog youtube để nói về vấn đề đó , các bạn theo dõi kênh của mình tại đây nhé:

How to build Robot Project in Frankfurt University Germany – YouTube