Posts

Showing posts with the label python

Why I learn QML and PySide2

sometimes I feel like that I want to automate something boring when I work on computer. so, I decided to do it! first of all, I investigated what I should do in order to automate something. the result is like below. I should learn scripting. however there are two type of script in desktop environment. such as shell in Linux and batch in Windows. if I learn one of them, I can do only in one platform. so, I choose to learn python. if I learn python, I can do scripting in both of platforms. and then, I don't want to make text base UI. so, I found PySide2. PySide2 is wrapper for Qt that is GUI framework. at last, I want to make automation tool fast and easily. QML is good option for that. QML is Qt Modeling Language. so, if I learn it, I think I can make GUI fast and easily. I just need simple widgets like label, text editor, button and so on. so, I will pick and learn them. if you also have the reason like me, this blog will be helpful. because I will update this b...

tuple type in python

Image
tuple is one of python sequence types. and tuple is immutable sequence type. "immutable" means that the data of tuple cannot be modified. at first, it would be good to see the declaration of tuple constructor. tuple([iterable]) and then, we can make tuple with some ways like below Using empty parentheses: () Separating items with commas: a, b, c or (a, b, c) Using the tuple() constructor or tuple(iterable) if you want to make empty tuple, do it like below t = () t = tuple () if you have some items to make tuple, do it like below t = ( 1 , 2 , 3 , 4 ) t = tuple ( 1 , 2 , 3 , 4 ) if you have any other sequence type data or customized iterable data to make tuple, do it like below sourceTuple = ( 1 , 2 , 3 ) sourceList = [ 1 , 2 , 3 ] t = tuple (sourceTuple) t = tuple (sourceList) it is very simple and easy to make tuple. let's try to make example code and see the result. t1 = () t2 = ( 1 , 2 , 3 , 4 ) t3 = tuple (t2) print (...

python list sort

Image
python list have sort method. so, I will introduce how to use the method with example code. the method declaration is like below sort(*, key=None, reverse=False)  if you don't pass any parameter, "sort" method will compare items with only < comparison operation. "key" parameter has to be function object. through this parameter, you can define the key value to be compared. if you use lambda expression, you can make the key parameter easier. but if I introduce it in this page, it is out of range of this subject. so I will introduce lambda later. "reverse" define the order of sorting items. simply, the evaluation of comparison is reversed or not. list1 = [ 2 , 3 , 5 , 1 , 6 , 7 ] list2 = [( 1 , 2 ),( 4 , 3 ),( 2 , 1 ),( 3 , 4 )] def comp1 (item): return item % 3 def comp2 (item): return item[ 1 ] list1 . sort() print (list1) list1 . sort(reverse = True ) print (list1) list1 . sort(key = comp1) # items iwll be sorted ...

python list type

Image
list type is mutable sequence type. I will introduce how to make list object in this page. there are some ways to make list object. using square brackets with empty between the brackets the expression is like below var = [] if with empty brackets, empty list will be made. using a list comprehension. the expression is like below var = [x for x in iterable] "iterable" means that something like sequence types. and technically, something that implement "__next__" method. I will introduce iterable and iterator type in the next subject. so, in this page, it will be fine to think it of like sequence type. back to the main subject, you can make list items using "for" expression. the syntax is like above example code. using the type constructor. everything is object in python. it means list is also object that has type constructor. as a result, you can make the list object using type constructor. example is ...

bitwise operations in python

Image
python also have bitwise operations. and the operations works with only integer type. the operations are like below x | y          bitwise or of x and y x ^ y        bitwise exclusive or of x and y x & y         bitwise and of x and y x << y        x shifted left by y bits x >> y        x shifted right by y bits the y value ( right-hand operand ) has to be positive value. otherwise, ValueError will be raised. ~x            the bits of x inverted if you cannot understand the terms such as "and", "or" and "exclusive", refer to wiki page-logical operation. let's check the example code. x = 1 y = 3 print ( "x | y = {}" . format(x | y)) print ( "x ^ y = {}" . format(x ^ y)) print ( "x & y = {}" . format(x & y)) print ( "x << y = {}" . format(x << y)) print ( "x >> y = {}" . for...

numeric type operations in python

Image
there are three numeric types in python int float complex python is dynamic typing language. so, we don't need to code the type explicitly. but, we can identify the type using "type" built-in function. x = 1 y = 3.6 print ( "x is {} type" . format( type (x))) print ( "y is {} type" . format( type (y))) there are operations for numeric types. I will introduce them in this page. x + y         sum of x and y x - y          difference of x and y x * y          product of x and y x / y          quotient of x and y x // y         floored quotient of x and y x % y         remainder of x / y -x              x negated +x            positive x abs(x)        absolute value or magnitude of x int(x)         x conve...

comparison operations in python

Image
there are 8 types comparison operations in python x < y x <= y x > y x >= y x == y x != y x is y x is not y < operation will return true if y is strictly greater than x. otherwise false. <= operation will return true if y is greater than x or equal to x. otherwise false. > operation will return true if y is strictly less than x. otherwise false. >= operation will return true if y is less than x or equal to x. otherwise false. and when you try to use above 4 operations, x and y have to be same data type. otherwise, TypeError will be raised. == operation will return true if x and y are same that means the value of them will be evaluated not object itself. != operation will return true if x and y are not same. otherwise, false. is operation will return true if x and y are same object. otherwise, false. is not operation will return true if x and y are not same object. otherwise false. I will show you example code to help you understand easier...

python logical operators

Image
there are three logical operators in python. if you have some experience about other programming languages, you might be familiar with ||, && and ! operation. but python has logical operations like below x and y x or y not x I think it looks more readable. and operation will return true if both x and y are true. otherwise false. or operation will return true if either x or y is true. if both of them are false, it will return false. lastly, not operation will reverse x. if x is false, it will be true. otherwise false. we can understand easier from example code. x = True y = False print ( "x is true and y is false then : {}" . format(x and y)) y = True print ( "x is true and y is true then : {}" . format(x and y)) print ( "x is true or y is true then : {}" . format(x or y)) x = False print ( "x is false or y is true then : {}" . format(x or y)) print ( "x is false and then not x : {}" . format( ...

how to strip string in python

Image
there is strip function in python. strip function strips string literally. there are 3 types of strip. str.rstrip([chars]) str.lstrip([chars]) str.strip([chars]) the way how to use is all the same but the direction which side will be stripped is different. chars parameter is characters to be stripped. if none, the default value is whitespace. I made example code. this will be helpful to understand the operation. str1 = " hello python " print (str1 . rstrip()) print (str1 . lstrip()) print (str1 . strip()) str2 = "aaaaaaahello pythonbbbbbbbb" print (str2 . lstrip( "a" )) print (str2 . rstrip( "b" )) print (str2 . strip( "ab" ))

isalpha, isdigit and isalnum functions in python

Image
we can check whether any string is consist of alphabet or numeric or both in python. let's learn each function one by one str.isalpha() this function will return true if all characters in the string are alphabet. otherwise, return false. str.isdigit() this function will return true if all characters in the string are digit. otherwise, return false. str.isalnum() this function is combined with isdigit and isalpha. if all characters are digit number or alphabet, this function will return true. otherwise return false. you can learn them well from code. str1 = "python" str2 = "python " str3 = "123" str4 = "python3" str5 = "python3.7" print ( '"python" -> isalpha: {}, isdigit: {}, isalnum: {}' . format(str1 . isalpha(), str1 . isdigit(), str1 . isalnum())) print ( '"python " -> isalpha: {}, isdigit: {}, isalnum: {}' . format(str2 . isalpha(), str2 . isdigit(), str2 . is...

string slicing in python.

Image
sequence types can be sliced in python. str is also immutable sequence type that means str objects can be sliced. so, I will introduce how to slice string in this page. I think slicing is very good tool in python. so, if you are familiar with this, it will be very helpful in the future. there are 3 types to slice string like below. s[i] s[i:j] s[i:j:k] i and j are index of string. k is the step. when you set only i, you can get ith character of string. and if you set i and j, you can get sub-string from i to j. if you set i, j and k, you can get sub-string from i to j with k step. i is included in the range. and j is not included in the range. the index can be negative value. for example, -1 means last one of string. -2 means second last one of string. let's check them in code. strSource = "hello python" print (strSource[ 0 ]) print (strSource[ 0 : 5 ]) print (strSource[ 0 : 12 : 2 ]) print () print (strSource[ - 1 ]) print (strSource[ 6 :...

how to use find function of str in python

Image
we can find the index of string that we want to find from the source string with str.find function. str.find(sub[, start[, end]]) the declaration is like above. sub is what you want to find. and you can also define the range from which the sub-string will be found. for example, there is "hello python". and each character has the index. the index begin from 0. as a result, 'h' will be index 0. and 'e' will be 1. and so on... return to the this page's subject, if you set start parameter to 6, and end to 12, the sub will be searched from index 6 to 12. further more, if you don't set start parameter, the default will be 0. and if you don't set end parameter, the default will be length of string. if sub is not found, the return value will be -1. let's check them in example code. 1 2 3 4 5 6 7 8 srcString  =   "hello python"   print (list(enumerate(srcString))) print ( "length of string :...

join strings with separator in python

Image
how can we join multiple string with any separator in python? actually, we need such this operation when we are programming. so, I will introduce how to join strings with separator in this page. there is a nice method in built-in type "str" in python. it is str.join(iterable) in order to join strings, you should make iterable that has strings witch you want to join with separator. but every element of iterable must be str type. otherwise, TypeError Exception will be raised. and "str" part will be separator string you want. I think it is the best way to understand something to see it. let's see it. 1 2 3 4 5 sep  =   " " strings  =  ( "my" , "name" , "is" , "simpp" )   print (strings) print (sep.join(strings)) cs

split string in python

Image
it's very easy to split string by any separator in python. I will introduce the way to split string in python. str that is built-in type in python has split method. str.split(sep=None, maxsplit=-1) you can pass any separator to sep parameter key. it can be also string like "<>". and there is another one for parameter, maxsplit. you can define how many string will be splited. the default value is -1 that means string will be splited at most. but if you set the parameter to any value, the string will be splited at most that value. for example, if you set the value to 2, the number of elements will be 3 at most as the result of operation. let's check them. 1 2 3 4 5 6 7 sampleString  =   "a,b,c,d,e" sampleString2  =   "1<>2<>3<>4<>5" print (sampleString.split( "," )) print (sampleString.split( "," , 2 )) print (sampleString2.split( "<>"...

python string literal declaration

Image
There are various ways to declare string literal in python. I will introduce them one by one in this page. First way is to use single quotes like 'string' 1 str1  =   'hello' cs Second way is to use double quotes like "string" 1 str1  =   "hello" cs Last way is to use triple quotes like """hello""" or '''hello''' 1 str1  =   '' 'hello' '' cs or 1 str1  =   "" "hello" "" cs Every way can declare string literal in python. but there are some differences. Triple quotes can declare multiple lines. all whitespace that is in triple quotes will be included in the string literal. Double quotes can include single quote in string and in contrary, single quotes can include double quote in string. so then, let's check them. 1 2 3 4 5 6 7 8 9 str1  =   ...

python string format function

Image
we can use the formatted string with format() function in python. the declaration is like below str.format(*args, **kwargs) let's check how to use the function in the example. in order to check it, I typed codes like below. 1 2 3 4 5 6 7 print ( "my favorite fruit is {}" . format ( "apple" ))   print ( "{}'s favorite fruit is {}" . format ( "apple" ,  "he" ))   print ( "{1}'s favorite fruit is {0}" . format ( "apple" ,  "he" ))   print ( "{human}'s favorite fruit is {fruit}" . format ( * * { "human" :  "she" ,  "fruit" :  "banana" })) cs you can learn something from above lines. we can set the format filed with braces {}. each argument has position and the position is used as index in braces {} dictionary can be argument and the key is used in each format filed.  it's time to chec...

declaration of variables in Python

Image
python is dynamic programming language. so, it is very simple and easy to declare variable in Python. you have to remember and type data types in static programming language such as C, C++ and JAVA. but it will be done with typing the name of variable in Python. let's try to declare variables. 1 2 3 4 5 6 7 8 9 10 number  =   1 string  =   "hello" mList  =  [ 1 , 2 , 3 , 4 ] mDict  =  { "name" :  "jack" ,  "age" :  25 }   print (number) print (string) print (mList) print (mDict)   Colored by Color Scripter cs I typed various typed variables such as string, number, list and dictionary like above. the result is like above. as you can see, in order to use variable in python, just type the variable name and assign any data to the variable.

how to pass arguments to python script

Image
if you want to pass arguments to script, you will be able to do it with this page. python script can get arguments from interpreter. but, in order to do it, you need built-in module "sys" "sys" has "argv" attribute. so, interpreter pass the arguments to "argv" attribute of "sys". let's try it. in order to get arguments, I created a script "main.py". and I coded like below. 1 2 3 4 import  sys print (sys.argv) cs as you can see, I imported "sys" and I printed out "argv" of "sys". in detail, "argv" is list data type. and in order to use attribute of any module, dot(.) has to be used between module name and attribute name. and then, I executed the script with some arguments. as you see above screen, the first argument is file name. and arguments the are passed from interpreter are begining from the second of list.

how to execute python script

Image
if you installed python, you can execute python script. I will introduce how to execute python script in page. first of all, we have to make python script to be executed. every file of which extension is ".py" can be python script file. so, I create "main.py" from notepad like below. if there is python script, we can execute it easily. first way to execute script, just double click like executing other programs. it will run if .py extension is associated with python. but, you can't see any result. because python interpreter will close the window when script is done. therefore, if you want see anything from that, you have to execute script from window command. this is second way. at first, open Windows command. then you can see below screen. and then, I moved to the location in which python script is. >cd (path) and then, type like below. >python main.py we can see "hello python"

how to check python version on windows

Image
in order to check python version, you have to open the command on windows. if you open command, type like below >>>python -v if you type "python -v", you can see more information about the build. or >>>python --version if you type "python --version", you can see simple version information like above screen.