Instantly share code, notes, and snippets.
jennyonjourney / gist:e4982d3fedd6c70f1da239f86f1918b7
- Download ZIP
- Star ( 4 ) 4 You must be signed in to star a gist
- Fork ( 0 ) 0 You must be signed in to fork a gist
- Embed Embed this gist in your website.
- Share Copy sharable link for this gist.
- Clone via HTTPS Clone using the web URL.
- Learn more about clone URLs
- Save jennyonjourney/e4982d3fedd6c70f1da239f86f1918b7 to your computer and use it in GitHub Desktop.
kusumamahesh123 commented Jun 14, 2020
thank you its working
Sorry, something went wrong.
yaswanth67 commented Jun 14, 2020 via email
arun95gangwar commented Jul 3, 2020
hrs = input("Enter Hours:") rate = input("Enter rate:") h=float(hrs) r=float(rate) def computepay(h,r): if h>40: fix=r h extra=(h-40) (r*0.5) pay=fix+extra
total=computepay(h,r) print("Pay",total)
AbhilashaSharma14 commented Jul 24, 2020
def computepay(h,r): if h<=40: pay=h r elif h>40: pay=40 r+(h-40) r 1.5 return(pay)
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) p = computepay(h,r) print(p)
my-name-arch commented Jul 27, 2020
input("Enter Hours:") h=float(hrs) rate=input("enter rate") r=float(rate) def computepay(h,r): if h<=40: pay=hr elif h>=40: pay=40r+(h-40)1.5r return (pay) p=computepay(h,r): print("Pay",p) I don;t see what is wrong with this code. It comes up as a parse error. Can someone please help
AhmedSaidi99 commented Aug 8, 2020
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) p = computepay(h,r) print("Pay", p)
MimAhmed commented Aug 12, 2020
hrs = input("Enter Hours:") hour_float = float(hrs) rate = input("Enter rate:") rate_float = float(rate) final_pay = computepay(hour_float ,rate_float) print(final_pay)
HaamzaHM commented Oct 17, 2020
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) p = computepay(h,r) print("Pay",p)
Japarna commented Oct 20, 2020
hrs= input("enter the hours:") rate=input("enter the rate per hour:") hrs=float(hrs) rate=float(rate) def computepay(hours,rate): if hrs <= 40: pay=hrs
pay=computepay(hrs,rate) print('Pay',pay)
JohnLeMay4 commented Oct 28, 2020
Why isn't this working? It is literally what Chuck did in the video and it works in my command prompt:
def computepay(hours, rate) : #print("In computepay", hours, rate) if hours > 40 : reg = rate * hours otp = (hours - 40.0) * (rate * 0.5) pay = reg + otp else: pay = hours * rate #print("Returning",pay) return pay sh = input("Enter Hours: ") sr = input("Enter Rate: ") fh = float(sh) fr = float(sr) xp = computepay(fh,fr)
print("Pay:",xp)
Why isn't this working? It is literally what Chuck did in the video and it works in my command prompt: def computepay(hours, rate) : #print("In computepay", hours, rate) if hours > 40 : reg = rate * hours otp = (hours - 40.0) * (rate * 0.5) pay = reg + otp else: pay = hours * rate #print("Returning",pay) return pay sh = input("Enter Hours: ") sr = input("Enter Rate: ") fh = float(sh) fr = float(sr) xp = computepay(fh,fr) print("Pay:",xp)
IT WAS THE COLON IN THE FINAL PRINT STATEMENT. I AM GOING TO SLEEP. Whew.
sisysl commented Jan 22, 2021
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate) def computepay(h,r): if h<=40: pay=h_r elif h>40: pay=40_r+(h-40)_r_1.5 return(pay) p = computepay(h,r) print('Pay',p)
not working
It is working, try again.
Be careful for indent when use function.
ozguripekci commented Jun 15, 2021
sooryansatheesh commented Jun 21, 2021
#Perfectly Working 💙 #function def computepay(hours, per_rate_hours): #overtime if (hours>40): pay = hours * per_rate_hours overtime = (hours - 40) * (0.5 * per_rate_hours) payment = pay + overtime else: payment = hours * per_rate_hours return payment #code begins hours = input("Enter Hours: ") per_rate_hours = input("Enter Per Rate Hour: ") #try and except try: f_hours= float(hours) f_per_rate_hours=float(per_rate_hours) except: print("Error") quit() final_pay = computepay(f_hours, f_per_rate_hours) print("Pay", final_pay)
You will get an error "You have to prompt for the data"...
def computepay(): hours = float(input("Enter Hours:")) rate_per_hour = float(input("Enter Rate per hour:")) if hours>40: pay=(40 rate_per_hour)+((hours-40) rate_per_hour 1.5) else :pay=(hours rate_per_hour) return pay
print("Pay",computepay()) quit()
my code above gives the exact answer but the autograder rejects it by telling that you must prompt for the data
MuhammadShayan17 commented Jul 19, 2021
it's because of the indent issue in line 6; the return function should be backwards, and with the de-indent apart from the above line.. Then I hope that the above code would have worked just right and fine.. :)
The only thing I find wrong in this programming code is the "indent" thing, as I think, you totally forgot about using indents and all..
Hmm, right, but along with this, I guess, there's also the indent thing issue here.. But nvm, all's good when the end's good I guess.. :/
def computepay(): hours = float(input("Enter Hours:")) rate_per_hour = float(input("Enter Rate per hour:")) if hours>40: pay=(40_rate_per_hour)+((hours-40)_rate_per_hour_1.5) else :pay=(hours_rate_per_hour) return pay print("Pay",computepay()) quit() my code above gives the exact answer but the autograder rejects it by telling that you must prompt for the data
Just look for the indent here, I guess. Apart from this, I think the autograder might also be not scanning and coding the double function you've put and typed out there, so yeaah...
ozguripekci commented Jul 19, 2021
Sometimes, "copy and paste" is not working. You need to write all code one-by-one on your IDE. And sometimes because of the "copy and paste", indent problems come through. Best wishes guys.
iamfusta commented Oct 31, 2021
#fixed TR_iamfusta
hrs = input("Enter Hours:") h = float(hrs) rate = input("Enter rate:") r = float(rate)
def computepay(h,r): if h<=40: pay=hr elif h>40: pay=40*r+(h-40) r 1.5 return(pay) p = computepay(h,r) print('Pay',p)
dynamodave789 commented Apr 21, 2022
def computepay(h, r): if h <= 40: pay = h * r elif h > 40: pay = (40*r+(h-40) 1.5 r) return(pay)
hrs = input("Enter Hours: ") h = float(hrs) rate = input("Enter Rate: ") r = float(rate) p = computepay(h, r) print("Pay", p)
eddshine commented May 6, 2022 • edited Loading
Here's my code: (Only 9 lines of code plus it's very easy to understand)
Have fun! :)
aliimran-ux commented Jun 16, 2022
This is perfect code 👍
sunnyfisher429 commented Jun 26, 2022
how can i fix this ?
hrs= input("Enter Hours:") rates=input("Enter hours:") h=float(hrs) r=float(rates)
def computepay(hrs,rates) if h>=40: p=(40+(fh-40)) r (fr 1.5) else h<=40: p=h r return(p)
sp=computepay(h,r) print ('Pay',p)
imranlondon commented Sep 17, 2022 • edited Loading
Pay and over time caculater.
def computepay(hours, rates): if hours <= 40: print(hours * rates) else: print ((hours - 40) * (rates * 1.5) + (40 * rates))
If hours are more than 40, the mean user did over time, and the rate differs from the actual rate. So, first, we count extra hours from 40, then multiply with different rates and then the original hours at the regular rate. Then combine both.
Taking input from user.
hours = float(input("Enter hours :")) rates = float(input("Enter rates :"))
called function
computepay(hours,rates)
sbedoyac commented Nov 4, 2022
Thaks for that comment, it was the solution
CristianoFIlho commented Dec 23, 2022
def computepay(hours, rate): if hours <= 40: return hours * rate else: overtime_hours = hours - 40 overtime_pay = overtime_hours * (rate * 1.5) return 40 * rate + overtime_pay
hours = float(input("Enter the number of hours worked: ")) rate = float(input("Enter the rate per hour: ")) gross_pay = computepay(hours, rate) print("Pay %.2f" % gross_pay)
programmarself commented Jun 23, 2023
its works 100%. def computepay(h,r): if h<=40: pay=h r elif h>40: pay=40 r+(h-40) r 1.5 return(pay)
silo3605 commented Jan 19, 2024 • edited Loading
This worked for me perfectly, most of the times it has to do with indentations, or Colons or " " improperly placed. def computepay(h, r): if hours <= 40: pay = hours * rate else: pay = 40 * rate + (hours - 40) * rate * 1.5 return pay
hours = float(input("Enter hours: ")) rate = float(input("Enter rate per hour: "))
p = computepay(10, 20) print("Pay", p)
Note: There are indentations in this code: if, else and returned must be aligned; pay, pay must also be aligned. If using python 3, hit the tab key once, which should place if right between f in def and space and c in computepay(h,r): The first pay should be right beneath hours of the if statement. The second pay should be beneath se of the else: and lastly return pay should be in alignment with else: so it should be if hours, else: and return pay aligned correctly and the same for pay by using the space bar in your pc. There two spaces after lines:6 and 10
Python Tutorial
File handling, python modules, python numpy, python pandas, python matplotlib, python scipy, machine learning, python mysql, python mongodb, python reference, module reference, python how to, python examples, python assignment operators.
Assignment operators are used to assign values to variables:
Related Pages
COLOR PICKER
Contact Sales
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]
Report Error
If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]
Top Tutorials
Top references, top examples, get certified.
Learning Python, 6th Edition by Mark Lutz
Get full access to Learning Python, 6th Edition and 60K+ other titles, with a free 10-day trial of O'Reilly.
There are also live events, courses curated by job role, and more.
Chapter 11. Assignments, Expressions, and Prints
Now that we’ve had a first introduction to Python statement syntax, this chapter begins our in-depth tour of specific Python statements. We’ll begin with the basics: assignment statements, expression statements, and print operations. We’ve already seen all of these in action, but here we’ll fill in important details we’ve skipped so far. Although they’re relatively simple, as you’ll see, there are optional variations for each of these statement types that will come in handy once you begin writing realistic Python programs.
Assignments
We’ve been using the Python assignment statement for a while to retain objects in examples. In its basic form, you write the target of an assignment on the left of an equals sign, and the object to be assigned on the right. The target on the left may be a name or object component, and the object on the right can be an arbitrary expression that creates an object. For the most part, assignments are straightforward, but here are a few key properties to note up front:
Assignments create object references. As discussed in Chapter 6 , Python assignments store references to objects in names or data structure components. They always create references to objects instead of copying the objects. Because of that, Python variables are more like pointers than data storage areas.
Names are created when first assigned. Python creates a variable name the first time you assign it a value (i.e., an object reference), ...
Get Learning Python, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.
Don’t leave empty-handed
Get Mark Richards’s Software Architecture Patterns ebook to better understand how to design components—and how they should interact.
It’s yours, free.
Check it out now on O’Reilly
Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems every day.
- Python Basics
- Interview Questions
- Python Quiz
- Popular Packages
- Python Projects
- Practice Python
- AI With Python
- Learn Python3
- Python Automation
- Python Web Dev
- DSA with Python
- Python OOPs
- Dictionaries
Different Forms of Assignment Statements in Python
We use Python assignment statements to assign objects to names. The target of an assignment statement is written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that computes an object.
There are some important properties of assignment in Python :-
- Assignment creates object references instead of copying the objects.
- Python creates a variable name the first time when they are assigned a value.
- Names must be assigned before being referenced.
- There are some operations that perform assignments implicitly.
Assignment statement forms :-
1. Basic form:
This form is the most common form.
2. Tuple assignment:
When we code a tuple on the left side of the =, Python pairs objects on the right side with targets on the left by position and assigns them from left to right. Therefore, the values of x and y are 50 and 100 respectively.
3. List assignment:
This works in the same way as the tuple assignment.
4. Sequence assignment:
In recent version of Python, tuple and list assignment have been generalized into instances of what we now call sequence assignment – any sequence of names can be assigned to any sequence of values, and Python assigns the items one at a time by position.
5. Extended Sequence unpacking:
It allows us to be more flexible in how we select portions of a sequence to assign.
Here, p is matched with the first character in the string on the right and q with the rest. The starred name (*q) is assigned a list, which collects all items in the sequence not assigned to other names.
This is especially handy for a common coding pattern such as splitting a sequence and accessing its front and rest part.
6. Multiple- target assignment:
In this form, Python assigns a reference to the same object (the object which is rightmost) to all the target on the left.
7. Augmented assignment :
The augmented assignment is a shorthand assignment that combines an expression and an assignment.
There are several other augmented assignment forms:
Similar Reads
- Different Forms of Assignment Statements in Python We use Python assignment statements to assign objects to names. The target of an assignment statement is written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that computes an object. There are some important properties of assignment in Python :- 3 min read
- Python | Priority key assignment in dictionary Sometimes, while working with dictionaries, we have an application in which we need to assign a variable with a single value that would be from any of given keys, whichever occurs first in priority. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This task can 4 min read
- Provide Multiple Statements on a Single Line in Python Python is known for its readability and simplicity, allowing developers to express concepts concisely. While it generally encourages clear and straightforward code, there are scenarios where you might want to execute multiple statements on a single line. In this article, we'll explore the logic, and 3 min read
- Difference between List VS Set VS Tuple in Python dynamic-sizedList: Lists are just like dynamic-sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. The main characteristics of lists are - The list is a datatype available in Python whi 3 min read
- Difference Between Integer and Float in Python Integers are used to represent whole numbers without any decimal points, floats, or floating-point numbers, accommodate values with decimal places. Understanding the differences between these data types is important for effective programming and data manipulation in Python. In this article, we will 3 min read
- Add Same Key in Python Dictionary In Python, dictionaries are a versatile data structure that allows you to store key-value pairs. If we want to add the same key with a new value to a dictionary, there are multiple methods to achieve this. In this discussion, we will explore different methods for adding the same key with a new value 3 min read
- Python | Min and Max value in list of tuples The computation of min and max values is a quite common utility in any programming domain be it development or any other field that includes any programming constructs. Sometimes, data can come in the format of tuples and min and max operations have to be performed in them. Let's discuss certain way 7 min read
- Append Elements to Empty List in Python In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list. 2 min read
- Python | Subtraction of dictionaries Sometimes, while working with dictionaries, we might have a utility problem in which we need to perform elementary operations among the common keys of dictionaries in Python. This can be extended to any operation to be performed. Let's discuss the subtraction of like key values and ways to solve it 6 min read
- Add Values into Empty List Using For Loop - Python Lists are versatile data structures that allow you to store and manipulate collections of items. The simplest way to add values to an empty list is by using append() method. This method adds a single item to the end of the list. [GFGTABS] Python a = [] # Loop through a range of numbers and add them 3 min read
- How to Update a Dictionary in Python This article explores updating dictionaries in Python, where keys of any type map to values, focusing on various methods to modify key-value pairs in this versatile data structure. Update a Dictionary in PythonBelow, are the approaches to Update a Dictionary in Python: Using with Direct assignmentUs 3 min read
- Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Let’s explore this approach. Using for loopThe simplest way to iterate over the characters in 2 min read
- Python Program to Re-assign a dictionary based on path relation Given a dictionary, the task is to formulate a python program to re-assign it using a path relation among its keys and values, i.e value of one key is key to another. Example: Input : test_dict = {3 : 4, 5 : 6, 4 : 8, 6 : 9, 8 : 10} Output : {3: 10, 5: 9, 4: 10, 6: 9, 8: 10} Explanation : key 3 has 4 min read
- Accessing Python Function Variable Outside the Function In Python, variables defined within a function have local scope by default. But to Access function variables outside the function usually requires the use of the global keyword, but can we do it without using Global. In this article, we will see how to access a function variable outside the function 4 min read
- Python | Accessing variable value from code scope Sometimes, we just need to access a variable other than the usual way of accessing by it's name. There are many method by which a variable can be accessed from the code scope. These are by default dictionaries that are created and which keep the variable values as dictionary key-value pair. Let's ta 3 min read
- Starred Expression in Python In Python, the starred expression is a feature that allows for the unpacking of elements from iterable like lists. This feature is particularly useful when the number of elements is variable or when working with function arguments. One common use case of the starred expression is to unpack elements 3 min read
- Python | Assign multiple variables with list values We generally come through the task of getting certain index values and assigning variables out of them. The general approach we follow is to extract each list element by its index and then assign it to variables. This approach requires more line of code. Let's discuss certain ways to do this task in 4 min read
- How to add values to dictionary in Python In this article, we will learn what are the different ways to add values in a dictionary in Python. Assign values using unique keysAfter defining a dictionary we can index through it using a key and assign a value to it to make a key-value pair. C/C++ Code veg_dict = {} veg_dict[0] = 'Carrot' veg_di 3 min read
- How Can Python Lists Transformed into other Data Structures Python, a versatile and widely used programming language, provides a rich set of data structures to accommodate various programming needs. One of the fundamental data structures in Python is the list, a dynamic array that allows the storage of heterogeneous elements. While lists are powerful, there 4 min read
- Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 [GFGTABS] Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) [/GFGTABS]Output: 6Explanation: The beauty of python list datatype is that within 3 min read
- Python Programs
- python-basics
IMAGES
COMMENTS
Dec 4, 2024 · The Walrus Operator in Python is a new assignment operator which is introduced in Python version 3.8 and higher. This operator is used to assign a value to a variable within an expression. Syntax: a := expression. Example: In this code, we have a Python list of integers. We have used Python Walrus assignment operator within the Python while loop.
Note: There are indentations in this code: if, else and returned must be aligned; pay, pay must also be aligned. If using python 3, hit the tab key once, which should place if right between f in def and space and c in computepay(h,r): The first pay should be right beneath hours of the if statement.
Implicit assignments in Python; These topics will take you through several interesting and useful examples that showcase the power of Python’s assignment statements. Annotated Assignment Statements. PEP 526 introduced a dedicated syntax for variable annotation back in Python 3.6.
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Since Python 3.8, code can use the so-called "walrus" operator (:=), documented in PEP 572, for assignment expressions. This seems like a really substantial new feature, since it allows this form of assignment within comprehensions and lambdas. What exactly are the syntax, semantics, and grammar specifications of assignment expressions?
this contains all the answers to the quizes and asssignments for "Programming for Everybody (Getting Started with Python)" on Coursera by the University of Michigan. - Coursera---Programming-for-Everybody-Getting-Started-with-Python-/Week 6- Assignment 4.6 at master · Ritik2703/Coursera---Programming-for-Everybody-Getting-Started-with-Python-
Assignments create object references. As discussed in Chapter 6, Python assignments store references to objects in names or data structure components. They always create references to objects instead of copying the objects. Because of that, Python variables are more like pointers than data storage areas. Names are created when first assigned.
May 10, 2020 · There are some important properties of assignment in Python :-Assignment creates object references instead of copying the objects. Python creates a variable name the first time when they are assigned a value. Names must be assigned before being referenced. There are some operations that perform assignments implicitly. Assignment statement forms ...
Solutions to the exercises of the popular Python specialisation in Coursera offered by the University of Michigan and taught by Dr. Chuck. This repository contains the solutions of the assignments of all 5 courses in the specialisation: 1.Programming for Everybody (Getting started with Python) 2.Python Data Structures 3.Using Python to Access Web Data 4.Using Databases with Python 5.Capstone ...
Assignment 6 – 100 points Lists What students will learn: 1) Declaring lists 2) Performing common operations on lists Overview: Lists are an incredibly powerful thing in computing. Almost every audio file, video file, and image you’ve ever seen on a computer is stored in a list-like structure. A list is simply a