רוצה ללמוד פייתון? הנה הקורס האינטראקטיבי החינמי שלנו בן 4 שעות

פייתון היא שפה פופולרית, תכליתית וקלה ללימוד. זו השפה המתאימה לבינה מלאכותית, למידת מכונה ומדע נתונים. יש האומרים שזו גם שפת התכנות הקלה ביותר להתחיל איתה.

אם זה נשמע כמו שפת תכנות שרוצים ללמוד, המשיכו לקרוא! במהלך הפסקאות הבאות אדריך אותך במהלך קורס פייתון אינטראקטיבי בחינם בן 4 שעות שהשקנו היום.

מה!? קורס פיתון בנושא Scrimba? עם אינטראקטיביות מלאה? ?

כן, זה נכון! הרגע השקנו? קורס פיתון בן 4 שעות הכולל המון אתגרי קידוד, הכל נוצר על ידי @OlofPaulson המצוין.

מעריכים מקושרים? # 100daysofcode # Python // t.co / 0CWun4z69B

- Scrimba (@scrimba) 3 במרץ 2020

קורס זה נועד לתת לך בסיס איתן הן בפייתון והן בתכנות בסיסי בכלל. זה נהדר למתחילים המחפשים דרך אינטראקטיבית ומרתקת ללמוד קוד.

הקורס נוצר על ידי המורה המבריק שלנו אולוף פולסון, שהוא אחד התומכים באקדמיית החאן בשוודית. אולוף הוא בעל רקע במימון, מנוסה בכתיבת אלגוריתמים, ויש לו תשוקה לחינוך פתוח ונגיש.

עכשיו בואו נסתכל על אופן המסלול!

1. מבוא לקורס

קורס זה מכסה את כל הנושאים הדרושים לך למתחילים למפתחי פיתון ביניים. זה עובר:

  • הפקת נתונים וזרימת התוכנית
  • מיתרים, משתנים
  • פעולות חשבון והשוואות
  • רשימות, צמרות, ערכות ומילונים
  • תנאים, אם ו- elifs
  • בזמן ולולאות
  • פונקציות / הצהרות החזרה
  • חפצים, חוגים וירושה
  • הבנות רשימה / מילון ופונקציות למבדה
  • מודולים

2. הפעלת פייתון על Scrimba עם בריתון

כדי לגרום לשפה אחורית כמו Python לרוץ בפלטפורמה חזיתית כמו Scrimba, אנו משתמשים בתוסף Brython index.htmlשבקובץ כדי להרכיב מחדש את קוד הפייתון ל- Javascript.

באופן כללי נשתמש brython.min.jsבגרסת ה- JS המינימלית ( ) אך לפונקציונליות רבה יותר, פשוט לבטל את ההערה לגרסת lib הסטנדרטית ( brython_stdlib.js).

ראוי לציין גם כמה מוזרויות של בריתון בסרימבה:

  1. input()התיבה היא JS פקוד ולא לראות את הגבס אבל עובדת כאשר מריץ קוד מקומי.
  2. לפעמים הדפדפן המינימלי של Scrimba מרחף בפינה במהלך כמה מדריכים - אתה לא צריך לדאוג בקשר לזה.

3. הדפסת הצהרה וזרימת תוכנית

כשאנחנו כותבים פייתון, לעתים קרובות אנו רוצים לבדוק שהוא פועל כצפוי. לשם כך אנו משתמשים print()בפקודה להפקת נתונים למסוף.

print('Welcome to Python 101!') 

הערה: המחשב קורא קוד מלמעלה למטה, כך שהוא מפעיל פקודות קרוב יותר לראש.

4. משתנים

משתנים משמשים לשמירת נתונים לשימוש בהמשך. אנו מצהירים על משתנים המשתמשים בשמם באותיות קטנות ואחריהם מפעיל ההקצאה הבסיסי ( =). שים לב שאם שם המשתנה מורכב ביותר ממילה אחת, יש להפריד אותם עם קו תחתון ( _).

failed_subjects="6" 

אנו משתמשים במשתנים בקוד שלנו על ידי הקדמתם עם סימן פלוס ( +).

print('Your son Eric is failing' + failed_subjects + ' subjects.') 

5. סוגי נתונים וחיזוי טיפוסים

סוגי הנתונים הבסיסיים של פייתון הם:

  • מיתרים - אלה מוקפים בשני מרכאות (יכולים להיות מרכאות כפולות או בודדות).
  • שלמים - אלה מספרים שלמים.
  • צף - מספרים עם נקודות עשרוניות.
  • בוליאנים - אלה מקבלים את הערך trueאו false.

כדי לברר באיזה סוג נתונים אתה משתמש, השתמש type()

print(type('hello')) 

הערה: אם ברצונך להשתמש במרכאות או אפוסטרופ במחרוזת, הקף את המחרוזת כולה במרכאות כפולות. לחלופין, תוכלו להשתמש בתו הבריחה של פייתון, שהוא הנטוי האחורי ( \)

a="it's" b="it\"s' 

ישנם כללים סביב ערבוב סוגי נתונים, למשל אי אפשר להכניס מספרים למחרוזת. Typecasting, או שינוי סוג, פותר זאת.

  • str() משנה נתונים למחרוזת.
  • int() משנה נתונים למספר שלם.
  • float() משנה נתונים לצוף.
print('Your son ' + name + ' is failing ' + str(failed_subjects) + ' subjects.') 

6. משתנים וסוגי נתונים - תרגיל

הגיע הזמן לאתגר Scrimba Python הראשון שלך! בקאסט זה תוכלו לבחון את הידע החדש שלכם על משתנים וסוגי נתונים. נסה לפתור את האתגר בעצמך ואז בדוק את הפתרון של אולוף כדי לראות אם אתה בדרך הנכונה.

7. פעולות חשבון

כאן אנו למדים כי פעולות החשבון הבסיסיות בפייתון הן חיבור, חיסור, כפל, חלוקת צף (המחזירה מספר נקודה צפה), חלוקת רצפה (שמעגלת את התוצאה למספר השלם הקרוב ביותר), מודולוס (המחזיר את שארית החישוב) ומעריך (המכפיל מספר בכוח של מספר אחר).

a=10 b=3 print('Addition : ', a + b) print('Subtraction : ', a - b) print('Multiplication : ', a * b) print('Division (float) : ', a / b) print('Division (floor) : ', a // b) print('Modulus : ', a % b) print('Exponent : ', a ** b) 

8. מיתרים - יסודות / חיתוך

בקאסט זה אנו למדים על כמה ממושגי היסוד בכל הנוגע לשימוש במיתרים.

  • הכפלת מחרוזות מאפשרת לנו להדפיס מחרוזת מספר פעמים. ישנן שלוש דרכים לעשות זאת (האחרונה מכניסה רווח בין מחרוזות):
print(msg+msg) print(msg*2) print(msg,msg) 

אנו יכולים לשנות את המקרה של המיתרים במספר דרכים:

  • upper() משנה את המחרוזת לאותיות רישיות.
  • lower() משנה את המחרוזת לאותיות קטנות.
  • capitalize() עושה שימוש באותיות רישיות במילה הראשונה של מחרוזת.
  • title() עושה שימוש באותיות רישיות בכל מילה במחרוזת.
print(msg.upper()) print(msg.lower()) print(msg.capitalize()) print(msg.title()) 

אנו יכולים להשתמש בפונקציות הבאות כדי לגלות מידע על מחרוזת:

  • print(len(msg))אומר לנו כמה תווים יש למחרוזת (במקרה זה, msg).
  • print(msg.count('Python')) מונה את מספר המקרים של מילה או אות - שים לב שהדבר תלוי רישיות.

אנו ניגשים למיתרים עם סוגריים מרובעים ( []).

print(msg[0]) 

הערה: מיתרי פייתון הם באינדקס 0, כלומר התו הראשון נספר כ 0 ולא 1. שימוש באינדקסים שליליים מתחיל את הספירה מסוף המחרוזת.

Returning parts of a string is known as slicing. Olof shows us how to slice with some examples:

  • print(msg[a:]) returns everything after the character in the position specified.
  • print(msg[a:b]) returns everything between the positions specified, not including the end position.
  • print(msg[:b]) returns everything until the position specified, again, not including the end position.

9. Exercise - Strings - Basics / Slicing

It's time for an exercise on strings. As well as allowing us to practice the skills we've learned in previous casts, this challenge also gets us thinking about a skill Olof hasn't explicitly told us:

Python 101 scope

Click the image to access the challenge.

This is a great intro to life as a bona fide programmer - don't forget that Google is your friend! Check out the rest of the cast to see how Olof did it.

10. Strings -2 Find/replace, String Formatting

This cast teaches us even more about working with strings.

  • We can create multi-line strings with triple quotes:
msg="""Dear Terry,, You must cut down the mightiest tree in the forest with… a herring! <3""" 

Olof runs us through some examples of working with strings:

print(msg.find('Python')) returns the position of the words or characters we search for (in this case, 'Python').

print(msg.replace('Python','Java')) allows us to replace words or characters in a string. Note that strings are immutable in Python, so to use the result of this function, you need to save it to a variable.

msg1=msg.replace('Python','C') 

print('Python' in msg) tells us whether a word or character exists in a string by returning true or false.

msg1 = f'[{name}] loves the color {color}!' allows us to format strings so that they are more readable.

11. User Input

In Python, we capture user input and print it like this:

name= input('What is your name?: ') print(name) 

This brings up a user input field which looks like this:

User input field in Python

12. User Input - Exercise

It's time to flex our programming muscles with a user input exercise! In this cast, you'll be building a distance converter using the hints below:

User inputs challenge hints

Click the image to access the challenge.

As usual, give the challenge a go on your own and then check Olof's solution to see whether you got it right.

13. Lists - Basics

Whereas a variable holds one piece of data, a list holds multiples pieces.

friends = ['John','Michael','Terry','Eric','Graham'] 

Lists are also zero-indexed, and can be accessed with square brackets, just like strings:

print(friends[1]) 

We can also use the same commands we used with strings to find out a string's length, find certain data within a string, etc.

print(friends[1],friends[4]) print(len(friends)) print(friends.coun('Eric')) 

14. Lists - continued

This cast takes us through a few essential skills for using lists, such as sorting (sort()), finding the sum, (print(sum()), appending new data (append('')), adding two lists together (.extend()), removing items (.remove('')), popping items (which removes an item but still allows you to return it) (.pop()), and clearing the list (.clear()).

15. Lists - Exercise

In this exercise, we will be having a go at manipulating lists.

Description of the challenge

Click the image to check out the solution.

Give it a go by yourself and then check out Olof's solution to check how it went.

16. Split and Join

This cast looks at splitting and joining parts of strings.

print(msg.split()) print('-'.join(friends_list)) 

17. Split and Join - Exercise

Here you'll use what you now know about splitting and joining to create a list of friends from a string.

As usual, have a go on your own and then check out Olof's solution to check your work.

18. Tuples

Tuples are lists that you can't change. They look the same as lists but are surrounded by parentheses instead of square brackets.

friends_tuple = ('John','Michael','Terry','Eric','Graham') 

You should use tuples instead of lists when you want to make sure that your data won't change in the course of your program running.

19. Sets

Sets are similar to lists and tuples but they're unordered and remove duplicates inside themselves. They are also very fast. Sets are surrounded by curly brackets.

friends_set = {'John','Michael','Terry','Eric','Graham','Eric'} 

In this cast, Olof takes us through some tips and tricks for using lists. Note: Creating an empty set works differently than creating an empty list or tuple:

#empty Lists empty_list = [] empyt_list = list() #empty Tuple empty_tuple = () empty_tuple = tuple() #empty Set empty_set = {} # this is wrong, this is a dictionary empty_set = set() 

20. Sets - Exercise

Here, we'll put our new-found knowledge of sets to the test. Take a look at the end of the cast to check your answer.

21. Comments

Comments are text in the code that Python ignores. They are mainly used for human-human communication, e.g. notes about the code, debugging or testing, and code documentation. Comments are preceded by the pound sign (#):

#Hiding in the comments 

22. Functions - Calling, Parameters, Arguments, Defaults

In this cast, Olof introduces us to functions - bundles of code which we can reuse later.

Functions are created (defined) with def and called with the function name plus parentheses ():

def greeting(): print("Hello Friend!") greeting() 

We also take another look at formatted strings, and how using them makes code more readable and more efficient.

def greeting(name,age=28): print("Hello " + name + ", you are " + str(age) + "!") print(f"Hello {name}, you are {age}!") 

23. Functions - Exercise

Here, Olof gives us the task of modifying and extending the functionality of an existing function. Give it a shot and then watch the rest of the cast to see if you're on the right track.

24. Functions - Named Notation

Named notation is the practice of naming arguments when calling a function so that the function definition understands which argument is which, even if they appear in a different order.

Profile(yob=1995,weight=83.5,height=192,eye_color="blue") 

25. Return statements

This cast takes us through return statements. A return statement allows us to get back our data after performing a function.

def value_added_tax(amount): tax = amount * 0.25 return tax 

Olof also takes us through a few handy ways of playing with our returned data, including creating strings, sets and tuples with it.

26. Comparisons and Booleans

This tutorial whizzes through some ways of comparing data, including equals (==), is not equal (!=), greater than (>), greater than or equal to (>=), less than (<), less than or equal to (<=), in (in), not in (not in), is (is) and is not (is not).

We also take a look at some Boolean properties and learn that false evaluates to 0 and true evaluates to 1. Empty objects and zeroes evaluate to false and everything else (strings, numbers except 0, and so on) evaluates to true.

27. Conditionals: If, Else, Elif

Conditionals allow us to run different code for different circumstances. if statements run if the function returns true, elif (else if) statements run if the functions returns true after another statement has returned false, and else statements run if none of the preceding statements have returned true, i.e. in all other eventualities.

is_raining = False is_cold = False print("Good Morning") if is_raining and is_cold: print("Bring Umbrella and jacket") elif is_raining and not(is_cold): print("Bring Umbrella") elif not(is_raining) and is_cold: print("Bring Jacket") else: print("Shirt is fine!") 

28. If/Elif/Else - Exercise

It's time to flex our conditional muscles with an exercise. We also get the chance to have a go at some extended functionality with a temperature converter.

As usual, go ahead and see if you can solve it yourself and check your answer against Olof's solution.

29. Conditionals - Exercise Improve

The cast gives us the chance to try out some optimization by shortening code and reducing the number of conditionals it contains.

There are many different ways of achieving this, so have a go on your own and then compare your answer to how Olof tackles it at the end of the cast.

30. While Loops

While loops are code which runs repeatedly until a condition tells it to stop. To create a loop, begin by asking yourself these questions:

What do I want to repeat?

What do I want to change each time?

How long should we repeat?

In the below example, we want to repeat adding one, we want to change i, and we want this to repeat until the number five is reached:

i=0 while i < 5: i+=1 print(f"{i}."+ "*"*i + "Loops are awesome" + "*"*i) 

31. While Loops - Exercise

It's time for an exercise on loops. In this challenge, we'll make a fun guessing game. Don't forget to consider the three loop questions in the previous chapter before you start, and then check the solution at the end of the cast to see how it went.

32. For Loops and Nesting

For loops allow us to execute a statement for each item in a string, list, tuple or set. For example, the following code prints every number between two and eight:

for number in range(2,8): print(number) 

It is also possible to nest loops inside other loops. The code below prints each of the numbers 1, 2 and 3 for every friend (John 1, John 2, John 3, Terry 1, Terry 2, Terry 3, etc.)

friends = ['John','Terry','Eric'] for friend in friends: for number in [1,2,3]: print(friend, number) 

In this cast, we also learn about a couple for handy for loop keywords, such as break and continue.

33. For loops - Exercise

It's time to test out what we have learned about for loops with an exercise. This time, we'll be making personalized party invitations. We also have a mini-challenge of including proper capitalization

for loops exercise description

Click the image to access the challenge.

As usual, see how it goes on your own and then check the solution to compare your answer.

34. Dictionaries

This cast introduces us to dictionaries. Dictionaries are used in Python to store key-value pairs, and they work in a similar way to a physical dictionary. You look up a word (the key) and get a definition or translation (the value) in return.

movie = { 'title' : 'Life of Brian', 'year' : 1979, 'cast' : ['John','Eric','Michael','George','Terry'] } 

Olof also runs us through how to change entries in a dictionary and add new ones, as well as how to handle the error message caused by users looking up entries which don't exist and how to run for loops over dictionary data.

for key, value in movie.items(): print(key, value) 

35. Sort() and Sorted()

Here, we learn about the difference between sort() and sorted(). While both functions sort the contents of a list, only sorted() returns the results.

We also look at the behavior of dictionaries, tuples and strings when they are sorted and see a brief introduction to lambda functions - one-line convenient throwaway functions, which we will see more on in a later tutorial. The lambda example below sorts the list according to the first entries:

my_list=[['car',4,65],['dog',2,30],['add',3,10],['bee',1,24]] print(sorted(my_list, key = lambda item :item[0])) 

36. Exceptions: Try/Except, Raise

In this cast, Olof takes us through some techniques for handling errors. We do this with try-except blocks, which look like this:

#try: #code you want to run #except: #executed if error occurs #else: #executed if no error #finally: #always executed 

37. Classes and Objects

Next up, Olof shows us classes and objects. There are four main concepts to understand when it comes to classes. These are classes, objects, attributes and methods.

Classes are blueprints which show us the structure of the data required. Objects contain the actual data we use. Attributes are variables within a class and methods are functions within a class.

We initialize a class with the initialization statement init:

class Movie: def __init__(self,title,year,imdb_score,have_seen): self.title = title self.year = year self.imdb_score = imdb_score self.have_seen = have_seen 

Note: By convention, we use the self keyword when naming attributes.

Having done this, we can now create instances of the class, as below:

film_1 = Movie("Life of Brian",1979,8.1,True) film_2 = Movie("The Holy Grail",1975,8.2,True) 

Methods are defined in classes as follows:

 def nice_print(self): print("Title: ", self.title) print("Year of production: ", self.year) 

There are two ways of calling methods (the output is the same):

film_2.nice_print() Movie.nice_print(film_2) 

38. Inheritance

In this cast, Olof shows us around the concept of inheritance. Inheritance allows us to use the methods from one class in another class without repeating all the code. We can then add further methods to differentiate the classes from each other.

class Person: def move(self): print("Moves 4 paces") def rest(self): print("Gains 4 health points") class Doctor(Person): def heal(self): print("Heals 10 health points") 

A class can inherit from multiple other classes. If the classes have different outputs for the same method, the class will choose the inherited method which is declared first.

In the example below, the Wizard class will inherit any shared methods from the Doctor and not the Fighter.

class Wizard(Doctor,Fighter): def cast_spell(self): print("Turns invisble") def heal(self): print("Heals 15 health points") 

39. Modules

Now it's time to look at modules. Modules are code snippets which you can import and use in the code. Some commonly-used ones provided by Python are datetime, random, string, os, math, browser and platform

To use modules, we first need to import Brython's standard lib version:

   -->

Modules are imported with the import keyword and can be given an easier-to-use alias with as:

import platform as pl print(pl.python_version()) 

40. Zip / Unzip

In this cast, Olof shows us how to zip and unzip objects. Zipping allows us to combine two or more iterable objects (strings, tuples, lists, etc.).

nums = [1,2,3,4] letters = ['a','b','c','d'] combo = list(zip(nums,letters)) print(combo) 

The example above turns the combined iterables into a list, but we could also turn it into a tuple, set or dictionary. Note: A dictionary is unsuitable when zipping more than two objects.

Unzipping allows us to assign results into separate variables, in this case num, let and nam.

num,let,nam =zip(*combo) 

41 Lambda Functions part 1

Here, we take a closer look at lambdas, or anonymous functions, which allow you to write single-line, throwaway function definitions which you might just use once. Compare the following:

Standard function:

def square(x): return x*x print(square(3)) 

Lambda:

square1 = lambda x: x*x 

Note: The return value in a lambda is implicit.

42. Lambda Functions Part 2

In this cast, we delve a little deeper into lambdas. Although we could always replicate a lambda with a standard function, there are some instances when lambdas are significantly better.

In this example, the return value of the lambda is a function, which gives us the ability to reuse the lambda for multiple different tasks. In the code below, we use a single lambda to multiply by two and five:

def func(n): return lambda a: a*n # a*2 doubler = func(2) print(doubler(3)) quintipler = func(5) print(quintipler(3)) print(type(func(3))) 

This cast also explains that we can call lambdas as soon as we create them.

print((lambda a,b,c: a+b+c)(2,3,4)) 

43 Lambda Functions - Exercise

Now it's time to practice creating a few of our own lambdas.

See how you get on and check your answers against Olof's solutions as you go along.

44. Comprehensions - Lists

Python comprehensions allow us to create lists, tuples, sets and dictionaries with less code. Note: Anything which can be created in a comprehension can also be created with a for loop, however the for loop requires more code.

Olof also provides a handy slide to give as a visual comparison of for loops and comprehensions in two different cases.

comparison of for loops and comprehensions

Click the slide to access the cast.

45. Comprehensions - Dictionary

Olof now shows us how to create a new dictionary using a comprehension. Were we to do this with a for loop, the code would look like this:

new_dict = dict() for movie, yr in zip(movies,year): new_dict[movie] = yr print(new_dict) 

With a comparison, it looks like this:

new_dict = {movie:yr for movie,yr in zip(movies,year)} print(new_dict) 

Much more concise and readable!

46. Randomness

This cast takes us through the random module and teaches us how to generate pseudo-random events. To use the module, we first import it:

import random 

We can then use the module to generate pseudo-random numbers:

for i in range(5): print(random.random()*6) 

Olof also shows us a variety of functions we can use with random.

Use of random is not limited to numbers though. We can also use it with iterables:

friends_list = ['John', 'Eric', 'Michael', 'Terry', 'Graham'] print(random.choice(friends_list)) 

Lastly, Olof shows us the string modules and we learn how to create pseudo-random words.

import random, string smallcaps = 'abcdefghijklmnopqrstuvwxyz' largecaps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' digits = '0123456789' letters_numbers = string.ascii_letters + string.digits word = '' for i in range(7): word += random.choice(letters_numbers) word1 = ''.join(random.sample(letters_numbers,7)) word = random.choices(letter_numbers, k=7) print(word) print(word1) 

Note: These words are not truly random and should therefore not be used to generate passwords.

47. Project - Crypto machine

Now we are nearing the end of the course, Olof gives us a big project to sink our teeth into. Take a look at the instructions, follow along and attempt to do each step on your own, before checking Olof's solution.

48. Project - Math Tutor

Our second project is to create a Math tutor which tutors us in multiplication tables. Take a look at the instructions below, have a go at the project and then check out how Olof does it.

Math Tutor project instructions

Click the image to access the challenge.

49. Course Summary

Congratulations on making it to the end of the Python 101 course! We have been over outputting data and program flow, strings, variables, arithmetic operations, comparisons, lists, tuples, sets, dictionaries conditionals, loops, functions, objects, classes, inheritance, comprehensions, lambdas and modules - so you should be proud of yourself!

Congratulations!

Don't forget you can always refer back to the course if you need to, or redo any exercises you felt unsure of them (or just particularly enjoyed!)

When you're ready to move on, Scrimba has a range of courses to teach you your next coding skill, so check them out!

go get it

Click the image to see Scrimba's range of courses

Happy coding! :)