Python Basic

W3School

64 flashcards · Programming Languages · by Daniel Blackbird

  1. print hello world

    print("hello,world")

  2. who make python and when ?

    Guido van Rossum, and released in 1991

  3. how space syntax work?

    if 5 > 2: print("Five is greater than two!") -------- error if 5 > 2: print("Five is greater than two!")

  4. one line comment in python

    #This is a comment

  5. multi line comment in python

    """ This is a comment written in more than just one line """

  6. int x =5 in python

    x=5 :))))

  7. int x=5 ; string x="name" in python

    x=5 x="name"

  8. different between x="name" x='name' in python?

    no difference

  9. int x=2; float y=2.2; complex z=2j; in python ?

    x=2; y=2.2; z=2j;

  10. how to know type of a variable by code in python?

    type(variable)

  11. y=12*10^4 in python and what type is this .

    y = 12E4 float

  12. int x = 1; how to convert it to float by code ?

    x = 1 # int y = 2.8 # float z = 1j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x)

  13. Print random number between 1 and 10 in python .

    import random print(random.randrange(1,10))

  14. 3 way casting a data to int float or string

    x = int(1) # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3 x = float(1) # x will be 1.0 y = float(2.8) # y will be 2.8 z = float("3") # z will be 3.0 w = float("4.2") # w will be 4.2 x = str("s1") # x will be 's1' y = str(2) # y will be '2' z = str(3.0) # z will be '3.0'

  15. assign a whole text to a varriable by code .

    #three = > " " " a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."""

  16. write a code print second word of "Hello World!" example : e

    a = "Hello, World!" print(a[1])

  17. write a code print Hello from "Hello World" phrase

    b = "Hello, World!" print(b[0:5])

  18. write a code that delete space from first and end of a string " Hello , World! "

    a = " Hello, World! " print(a.strip()) # returns "Hello, World!"

  19. write a code that show length of a string

    a = "Hello, World!" print(len(a))

  20. write a code that convert to lowercase string (Hello,World)

    a = "Hello, World!" print(a.lower())

  21. write a code that convert to uppercase string (Hello,World)

    a = "Hello, World!" print(a.upper())

  22. write a code that Replace a word of string to other word . example:Hello World! => (H to J ) => Jello World

    a = "Hello, World!" print(a.replace("H", "J"))

  23. write a code that split a strings to substrings example : Hello World! => returns ['Hello', ' World!']

    a = "Hello, World!" print(a.split(","))

  24. write a code that combine two variables(string,integer) example : age = 21 , i am [age] years === i am 21 years old

    age = 36 txt = "My name is John, and I am {}" print(txt.format(age))

  25. write a code that combine string and integer but chose order by string

    quantity = 3 itemno = 567 price = 49.95 myorder = "I want to pay {2} dollars for {0} pieces of item {1}." print(myorder.format(quantity, itemno, price))

  26. Python Arithmetic Operators

    + - * / % // **

  27. Python Assignment Operators

    = += -+ *= /= %= //= **= &= |= ^= >>= <<=

  28. Python Comparison Operators

    == != > < >= <=

  29. Python Logical Operators

    and or not

  30. Python Identity Operators

    is is not

  31. Python Membership Operators

    in not in

  32. Python Bitwise Operators

    & | ^ ~ << >>

  33. write a code that make a list contains [apple, banana, cherry]

    thislist = ["apple", "banana", "cherry"] print(thislist)

  34. create a list and print first element

    thislist = ["apple", "banana", "cherry"] print(thislist[1])

  35. create a list and change first element and and print first element

    thislist = ["apple", "banana", "cherry"] thislist[1]="moooz" print(thislist[1])

  36. write a code that make a list and print whole list by for loop

    thislist = ["apple", "banana", "cherry" , "maskhare"] for x in thislist: print(x)

  37. write a code that if item1 exist in list print yes.

    thislist = ["apple", "banana", "cherry"] if "apple" in thislist: print("Yes, 'apple' is in the fruits list")

  38. write a code that make a list and print length of list

    thislist = ["apple", "banana", "cherry"] print(len(thislist))

  39. write a code that make a list and add a item to it

    thislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)

  40. write a code that make a list then add a item to a index of list

    thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)

  41. write a code that make a list and remove a string from it example : apple orange banana remove orange from it

    thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)

  42. write a code that make a list and pop a first item and last item from it .(without know index of last item) example :list = apple , banana ,cherry print=> banana

    thislist = ["apple", "banana", "cherry"] thislist.pop() thislist.pop(0) print(thislist)

  43. write a code that make a list and remove a item by index.

    thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)

  44. write a code that make a list and remove list completely

    thislist = ["apple", "banana", "cherry"] del thislist

  45. write a code that make a list and clear list items

    thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)

  46. can i use list2=list1 as copy list1 to list2?why?

    no,because when we use this code ,list2 will reference to list1 and when we change list2 ,it will changes list1 because it has same address

  47. write a code that make a list and copy that list to another list. two method

    thislist = ["apple", "banana", "cherry"] mylist = thislist.copy() print(mylist) -------- thislist = ["apple", "banana", "cherry"] mylist = list(thislist) print(mylist)

  48. write a code that make a list with list() constructor

    thislist = list(("apple", "banana", "cherry")) # note the double round-brackets print(thislist)

  49. write a code that make a tuple and print it

    thistuple = ("apple", "banana", "cherry") print(thistuple)

  50. what is tuple in python?

    A tuple is a collection which is ordered and unchangeable.

  51. write a code that print second items in tuple.

    thistuple = ("apple", "banana", "cherry") print(thistuple[1])

  52. write a code that make a tuple and print it by for loop

    thistuple = ("apple", "banana", "cherry") for x in thistuple: print(x)

  53. write a code that make a tuple and check condition if answer is true print "yes"

    thistuple = ("apple", "banana", "cherry") if "apple" in thistuple: print("Yes, 'apple' is in the fruits tuple")

  54. write a code that make a tuple and print length of tuple

    thistuple = ("apple", "banana", "cherry") print(len(thistuple))

  55. is it possible add item to tuple?change tuple item?remove item from tuple ?

    no,tuple is unchangeable

  56. is it possible that remove tuple completely ?if yes how?

    thistuple = ("apple", "banana", "cherry") del thistuple print(thistuple) #this will raise an error because the tuple no longer exists

  57. write a code that make a tuple by constructor

    thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets print(thistuple)

  58. what is set in python?

    A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

  59. write a code that make a set and print it

    thisset = {"apple", "banana", "cherry"} print(thisset)

  60. write a code that make a set and print it by loop

    thisset = {"apple", "banana", "cherry"} For x in thisset print x ------- result may change every times .

  61. write a code that make a set and and print True Or False if a item available in set or not .

    thisset = {"banana","apple","orange"} print("bananna" in thisset)

  62. write a code that make a set and update it by addition info exp:set= apple ,orange,bannana update= apple,orange ,banana ,cherry

    thisset("apple","orange","banana") thisset.update(["apple","orange","banana","cherry"]) print(thisset)

  63. write a code that make a set and print length of set

    thisset("apple","orange","banana") print(len(thisset))

  64. write a code and make a set and then remove it by two way . what different between to ways?

    thisset("apple","orange","banana") thisset.discard() thisset.remove() ------------------------------------------------- remove => if set not available then error occur but discard no error